Send JSON-Request to Flask via Curl
31,938
According to the get_json
docs:
[..] function will return
None
if the mimetype is notapplication/json
but this can be overridden by theforce
parameter.
So, either specify the mimetype of the incoming request to be application/json
:
curl localhost:5000/post -d '{"foo": "bar"}' -H 'Content-Type: application/json'
or force JSON decoding with force=True
:
data = request.get_json(force=True)
If running this on Windows (cmd.exe
, not PowerShell), you'll also need to change the quoting of your JSON data, from single quotes to double quotes:
curl localhost:5000/post -d "{\"foo\": \"bar\"}" -H 'Content-Type: application/json'
Related videos on Youtube
Author by
OhMad
Updated on November 01, 2020Comments
-
OhMad over 3 years
I setup a very simple post route in flask like this:
from flask import Flask, request app = Flask(__name__) @app.route('/post', methods=['POST']) def post_route(): if request.method == 'POST': data = request.get_json() print('Data Received: "{data}"'.format(data=data)) return "Request Processed.\n" app.run()
This is the curl request I am trying to send from the command-line:
curl localhost:5000/post -d '{"foo": "bar"}'
But still, it prints out 'Data Received: "None"'. So, it doesn't recognize the JSON I passed it.
Is it necessary to specify the json format in this case?
-
OhMad over 6 yearsStill not working, unfortunately.
-
randomir over 6 yearsHave you seen the edit of question? I thought you had a typo, but maybe that was also an error in your code. Check your print line uses format:
print('Data Received: "{data}"'.format(data=data))
. -
OhMad over 6 yearsOh, I am just using the new Python 3.6 feature instead of the format method where you put an 'f' in front of your string and can use the variable directly inside of it :)
-
randomir over 6 yearsIn case you wanted to use the new interpolated strings, you missed the f prefix..
-
OhMad over 6 yearsGuess I removed it from the code above, but I have it on my machine. Still, it's not functioning with the f, nor with the format method. So, I guess that's not the problem in this case :)
-
randomir over 6 yearsWhat version of flask/curl do you use? On what platform?
-
OhMad over 6 yearsI use curl 7.56.1 and Flask 0.12.2 on Windows
-
OhMad over 6 yearsI fixed it. I guess it's a Windows specific problem with the double quotes. curl localhost:5000/post -d "{\"foo\": \"bar\"}" -H "Content-Type: application/json" works just fine.
-
randomir over 6 yearsGlad to hear that. So it works even without the tips I gave you in my answer?
-
OhMad over 6 yearsNope, had to add the content type as well. So, thank you!
-
randomir over 6 yearsThat makes sense. You're welcome!
-
user305883 about 5 yearsfor me it was a combination of things: I was printing request.form : that was read as a string, need to include content type on curl, and instead of reading fields and values like :
fields = [k for k in request.form]
I've got to userequest.get_json()
. But whyfields = [k for k in request.form]
was not parsed properly, despite content type application/json ? -
randomir about 5 years@user305883,
request.form
contains form values. When you use JSON body encoding, that's not form. See an intro to forms here.