Rectangle 27 0

json Sending data through a Response object in Python Flask?


from flask import jsonify

@app.route('/login', methods=['POST'])
def login():
    data = {'name': 'nabin khadka'}
    return jsonify(data)
return jsonify(data), 200

Simply use jsonify() from flask package. This method takes any serializable data type (for example I have used data in the following example).

To return a status code, simply return it at the end of the return as below:

Note
Rectangle 27 0

json Sending data through a Response object in Python Flask?


from flask import Flask, json

@app.route('/login', methods=['POST'])
def login():
    data = {"some_key":"some_value"} # Your data in JSON-serializable type
    response = app.response_class(response=json.dumps(data),
                                  status=200,
                                  mimetype='application/json')
    return response

Don't build JSON responses manually like this. That's what jsonify is for.

Thank you very much. And it is received as json.loads(response.content)

The response data content type is defined by mimetype parameter.

What is the difference? By the way, how would you include the status?

You can append the data to the response like this:

Note