Send JSON-Request to Flask via Curl

31,938

According to the get_json docs:

[..] function will return None if the mimetype is not application/json but this can be overridden by the force 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'
Share:
31,938

Related videos on Youtube

OhMad
Author by

OhMad

Updated on November 01, 2020

Comments

  • OhMad
    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
    OhMad over 6 years
    Still not working, unfortunately.
  • randomir
    randomir over 6 years
    Have 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
    OhMad over 6 years
    Oh, 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
    randomir over 6 years
    In case you wanted to use the new interpolated strings, you missed the f prefix..
  • OhMad
    OhMad over 6 years
    Guess 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
    randomir over 6 years
    What version of flask/curl do you use? On what platform?
  • OhMad
    OhMad over 6 years
    I use curl 7.56.1 and Flask 0.12.2 on Windows
  • OhMad
    OhMad over 6 years
    I 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
    randomir over 6 years
    Glad to hear that. So it works even without the tips I gave you in my answer?
  • OhMad
    OhMad over 6 years
    Nope, had to add the content type as well. So, thank you!
  • randomir
    randomir over 6 years
    That makes sense. You're welcome!
  • user305883
    user305883 about 5 years
    for 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 use request.get_json(). But why fields = [k for k in request.form] was not parsed properly, despite content type application/json ?
  • randomir
    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.