Send JSON to Flask using requests Send JSON to Flask using requests flask flask

Send JSON to Flask using requests


You are not sending JSON data currently. You need to set the json argument, not data. It's unnecessary to set content-type yourself in this case.

r = requests.post(url, json=event_data)

The text/html header you are seeing is the response's content type. Flask seems to be sending some HTML back to you, which seems normal. If you expect application/json back, perhaps this is an error page being returned since you weren't sending the JSON data correctly.

You can read json data in Flask by using request.json.

from flask import request@app.route('/events', methods=['POST'])def events():    event_data = request.json


If you use the data argument instead of the json argument, Requests will not know to encode the data as application/json. You can use json.dumps to do that.

import jsonserver_return = requests.post(    server_ip,    headers=headers,    data=json.dumps(event_data))