[CLOSED] Anvil.http.request // Invalid JSON in response

Hi
I have this little piece of server code running on Basic Python 3 environment:

  R = anvil.http.request(url=URL,
                method="POST",
                data=PARAMS_0, json=True)

I get the error

HttpErrorStatus: Invalid JSON in response
at /libanvil/anvil/http.py, line 51

In order to debug I modify to:

  R = anvil.http.request(url=URL,
                method="POST",
                data=PARAMS_0)

  DATA = R.get_bytes()
                         
  print(DATA)

and get the printout:

b'{"error":{"code":"readapidenied","info":"You need read permission to use this module.","*":"See https://mywiki.myhost.com/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce> for notice of API deprecations and breaking changes."}}'

With Postman, too:

{
    "error": {
        "code": "readapidenied",
        "info": "You need read permission to use this module.",
        "*": "See https://mywiki.myhost.com/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce> for notice of API deprecations and breaking changes."
    }
}

That looks like valid JSON to me.
What’s wrong?

Thanks and BR

Thanks @aldo.ercolani, that is indeed valid JSON. Looks like a bug. Moving to Bug Reports.

1 Like

Hi @shaun, this little annoying bug is still in place, isnt’ it?

Hi @aldo.ercolani, do you have an example of the URL or a clone link I can look into?

Hi @bridget,
here’s a copyapp.

https://anvil.works/build#clone:KFQMP5YMMZNC7M2B=CAPNIZGSINVVWOHFUVOCZZL2

Best regards

Remember that setting json=True also encodes the data you send as JSON rather than the default of application/x-www-form-urlencoded. The API you are using can’t cope with this, so returns an error. You can see from the content attribute of the Exception that it is actually returning HTML in this case. That obviously can’t be decoded as JSON, hence the Exception.

If you need to make an assymetrical HTTP requests like this, you will need to manually encode or decode the JSON for the request or from the response as required.

5 Likes

Ok I see now.
Thanks and BR