Flask Receiving JSON from an HTTP Request


If the mimetype of the HTTP request is application/json, calling request.get_json() will return the parsed JSON data (otherwise it returns None)

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/api/echo-json', methods=['GET', 'POST', 'DELETE', 'PUT'])                                                                                                    
def add():                                                                                                                              
    data = request.get_json()
    # ... do your business logic, and return some response
    # e.g. below we're just echo-ing back the received JSON data
    return jsonify(data)

Try it with curl

The parameter -H 'Content-Type: application/json' specifies that this is a JSON request:

 curl -X POST -H 'Content-Type: application/json' -d '{"name": "Alice"}'               
  "name": "Alice"

To send requests using other HTTP methods, substitute curl -X POST with the desired method e.g. curl -X GET, curl -X PUT, etc.