How to send and receive data from flask?
OK - a few issues here:
First, you can use requests.get_json() to retrieve your JSON data at the server end:
from flask import Flaskfrom flask import requestimport jsonapp = Flask(__name__) @app.route('/determine_escalation/', methods = ['POST'])def determine_escalation(): jsondata = request.get_json() data = json.loads(jsondata) #stuff happens here that involves data to obtain a result result = {'escalate': True} return json.dumps(result)if __name__ == '__main__': app.run(debug=True)
Also, when you are putting your data together, rather than using "data=s" to send the request, use "json=s":
import sysimport jsonimport requestsconv = [{'input': 'hi', 'topic': 'Greeting'}]s = json.dumps(conv)res = requests.post("http://127.0.0.1:5000/determine_escalation/", json=s).json()print(res['escalate'])
Please note that I have added trailing slashes at the end of the URL - this is just good practice :-)
I've also incorporated MarcelK's suggested changes - removing the quotes from the boolean 'True' (server side) and using .json() to parse the response on the client side - both of these are great suggestions.
I've tested this revised version (and re-revised version) and it works fine.