Python Flask - Request.json Returns None Type Instead Of Json Dictionary
I'm writing a very simple demo webapp, and I can't seem to pass a json object from js to python using ajax. I've tried a number of suggestions from people on so with similar proble
Solution 1:
Because you are not sending JSON, to the flask app (see this and this). Your current code results in a standard urlencoded form post. Which in turn results in an entry being populated in request.form
request.form.get('word')
Switch to a json post as per the guidelines in the above Q&As to access the data through request.json.
Solution 2:
the data is likely not flowing to json if you are getting None
, so you should jsonify the data. It will be coming in in the form of form
.
from flask import jsonify
@app.route("/examplemethod", methods=['POST'])defexample_method():
data = jsonify(request.form).json
print(data) #will be in the form a json dictreturn data['foo-key'] #can retrieve specific items with their key-pairs
Post a Comment for "Python Flask - Request.json Returns None Type Instead Of Json Dictionary"