The documentation says to use request.get_json()
, but that causes the service to return an error:
Server returned HTTP response code: 500
calling request.data
or request.json
both work, however.
Stack trace:
Traceback (most recent call last):
File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/home/blake/ves/p27/lib/python2.7/site-packages/werkzeug/contrib/fixers.py", line 125, in __call__
return self.app(environ, start_response)
File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/home/blake/ves/p27/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/blake/workspace/starcycle-flask/starcycleweb.py", line 17, in api
print request.get_json()
File "/home/blake/ves/p27/lib/python2.7/site-packages/werkzeug/local.py", line 336, in __getattr__
return getattr(self._get_current_object(), name)
AttributeError: 'Request' object has no attribute 'get_json'
I thought get_json is was the preferred way to get the json data.
get_json. Parses the incoming JSON request data and returns it. By default this function will return None if the mimetype is not application/json but this can be overridden by the force parameter.
from flask import Flask from flask import request app = Flask(__name__) @app. route('/users/<user_id>', methods = ['GET', 'POST', 'DELETE']) def user(user_id): if request. method == 'GET': """return the information for <user_id>""" . . . if request.
from flask import request @app.route('/') def index(): username = request.cookies.get('username') # use cookies.get(key) instead of cookies[key] to not get a # KeyError if the cookie is missing. Note that cookies are set on response objects.
In Flask/wrappers.py the method get_json()
is defined as method to the class Request
.
However, the same file still contains the old, deprecated method json()
. If you have an old version of flask, then a) update or b) use request.json()
instead.
I had similar error with the flask Response object, it ended up being a version issue, we used flask 0.12.1 and get_json() is available on 1.0.2
Docs:
http://flask.pocoo.org/docs/0.12/api/#response-objects
VS
http://flask.pocoo.org/docs/1.0/api/#response-objects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With