Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User gets API error "Did not attempt to load JSON data because the request Content-Type was not 'application/json'" #152

Closed
schuderer opened this issue Apr 6, 2022 · 5 comments
Labels
bug Something isn't working dependencies Pull requests that update a dependency file help wanted Extra attention is needed

Comments

@schuderer
Copy link
Owner

  • ML Launchpad version: 1.1.0
  • Model Type used: Python
  • DataSource type(s) used: n/a
  • Python version: 3.8.12 (Flask 2.0.2/2.1.1, Flask-Restful 0.3.9)
  • Operating System: Win

Description

After setting up a new Python environment using Python 3.8 instead of Python 3.6 and reinstalling the dependencies (among which are Flask 2.1.1 and Flask-Restful 0.3.9), calling the API gives the user the error (in JSON): Did not attempt to load JSON data because the request Content-Type was not 'application/json'

MLLP APIs used to accept both GET and POST, with Form/Param data or a json body.

Tried out several permutations of the RAML definitions and GET/POST requests with different kinds of param/data using the requests package and got different errors in the process (The method is not allowed, Cannot parse JSON body null), and what ended working in the end was to:

  • Use a GET request
  • Pass the dict with the parameters as data=...
  • Set the Content-Type header to application/json explicitly

Steps to reproduce

I could not reproduce this yet on my own system. I have an MLLP project there using the same versions of everything and I did not get any errors.

@KJWeterings
Copy link

To add to the description by @schuderer:

What ended working in the end was to:

  • Use a GET request
  • Pass the dict with the parameters as data=...
  • Set the Content-Type header to 'application/json' explicitly
  • Wrap the args_dict in json.dumps(), so data = json.dumps(args_dict)

@KJWeterings
Copy link

KJWeterings commented Apr 6, 2022

We found the issue. Apparently the newest version of Werkzeug (2.1) created the error message. When using version 2.0, it should be working smoothly again.

@p-miralles
Copy link

p-miralles commented Apr 18, 2022

Hi all! Confirming here both problem and solution. After days of searching, making a downgrade to Werkzeug 2.0.3 was the right answer. Thanks a lot!

@schuderer schuderer added bug Something isn't working dependencies Pull requests that update a dependency file help wanted Extra attention is needed labels Apr 21, 2022
@schuderer
Copy link
Owner Author

schuderer commented Apr 21, 2022

Thank you @KJWeterings and @p-miralles for finding and confirming that pinning Werkzeug 2.0.3 is another workaround (besides changing the type of request, which is not really acceptable in most cases).

@ all: Keep in mind that Werkzeug is only meant to be used for debugging (e.g. using the mllaunchpad api command). That's why a third potential (untested) workaround could be to do your debugging using a production-quality WSGI server like waitress or gunicorn. Waitress should work on all operating systems:

pip install waitress
waitress-serve --listen=localhost:5000 --threads=1 mllaunchpad.wsgi:application

I'm not sure yet about the underlying causes of the error(s) -- while we could simply change mllaunchpad's debugging API to always use waitress instead, we don't know the root cause, and the underlying change might be something that is currently percolating through the whole WSGI ecosystem and might hit us again at a later time. Werkzeug 2.1 deprecated a lot of stuff, and something in that list appears to bite us via our Flask or Flask-RESTful dependencies. I somehow suspect that the use of Flask-RESTful might not be particularly future-proof at this time, either. ;) But before rewriting our API module to replace it, it would be useful to find out what triggers this error exactly, and if this is in fact happening in Flask-RESTful. Any help is welcome!

Edit: related: #140
Edit 2: It's probably worth a try to look at flask-restx (direct fork of flask-restful), which might be a drop-in replacement whose (as-yet open) PRs python-restx/flask-restx#427 and python-restx/flask-restx#423 may fix our issues.

@schuderer
Copy link
Owner Author

Closed by workaround (#154 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dependencies Pull requests that update a dependency file help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants