appdaemon icon indicating copy to clipboard operation
appdaemon copied to clipboard

AttributeError: 'dict' object has no attribute 'json'

Open electrofloat opened this issue 2 years ago • 2 comments

What happened?

I've just updated to 4.4.2, and noticed that my webhook stopped working with the following error:

2023-06-17 11:43:53.664502 ERROR AppDaemon: Traceback (most recent call last):
  File "/home/homeassistant/appdaemon/lib/python3.10/site-packages/appdaemon/http.py", line 869, in call_app_endpoint
    ret, code = await self.dispatch_app_endpoint(endpoint, request)
  File "/home/homeassistant/appdaemon/lib/python3.10/site-packages/appdaemon/http.py", line 948, in dispatch_app_endpoint
    return await callback(args, rargs)
  File "/home/homeassistant/.appdaemon/apps/webhooks/webhooks.py", line 8, in webhook_callback
    data = await request.json()
AttributeError: 'dict' object has no attribute 'json'

Reading the docs here for 4.4.2: https://appdaemon.readthedocs.io/en/4.4.2/AD_API_REFERENCE.html#appdaemon.adapi.ADAPI.register_endpoint

It shows that this is how to use the callback:

async def alexa_cb(self, request, kwargs):
    data = await request.json()
    self.log(data)
    response = {"message": "Hello World"}
    return response, 200

But upon inspecting the request with self.log(request) I can see that it already contains the data I need. So I had to modify my code to just something like this:

async def alexa_cb(self, request, kwargs):
    data = request
    self.log(data)
    response = {"message": "Hello World"}
    return response, 200

So awaiting on the request.json() is no longer needed but the docs has not been updated, or I'm doing something wrong?

Version

4.4.2

Installation type

Python virtual environment

Relevant log output

No response

Relevant code in the app or config file that caused the issue

No response

Anything else?

No response

electrofloat avatar Jun 17 '23 09:06 electrofloat

Yes, that’s correct, it was changed in the last release to make the async and non-async versions more consistent. I guess it didn’t make it into the docs, I’ll make a not to add it.

acockburn avatar Jun 17 '23 11:06 acockburn

Just ran into this issue myself. I think ece502d257c1a53110c0837a6819eafeab3c11d3 was meant to address this, but it appears to not work for async methods.

For my use-case, I need access to the request.remote field. I don't think the http request is accessible at all in async methods now.

esev avatar Sep 18 '23 02:09 esev