Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send and receive data from flask?

I am trying to send a python dictionary created from client.py to my webservice, have the webservice do something to the data, and return a boolean to client.py. This is the code I have so far for the server and client:

Server side (inside webservice.py):

from flask import Flask
from flask import request
import json

app = Flask(__name__) 

@app.route('/determine_escalation',methods = ['POST'])
def determine_escalation():
    jsondata = request.form['jsondata']
    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)

Client side (inside client.py):

import sys
import json
import requests

conv = [{'input': 'hi', 'topic': 'Greeting'}]
s = json.dumps(conv) 
res = requests.post("http://127.0.0.1:5000/determine_escalation",data=s)
print res.text

But when I print out res.text, I get this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>

What am I doing wrong, and how can I fix this? New to Flask and JSON stuff so any help is appreciated.

like image 293
ilikecats Avatar asked Feb 26 '16 20:02

ilikecats


People also ask

How do I get data from a Flask POST request?

Solution. The request module allows you to access the data passed into your Flask application through an HTTP request. The data within a request can be accessed through the request module's properties.

How do I get a POST on the Flask?

GET and POST requestsEnter the following script in the Python shell. Once the development server is up and running, open login. html in the browser, enter the name in the text field, and then click Submit. The form data will POST to the URL in the action clause of the form label.


1 Answers

OK - a few issues here:

First, you can use requests.get_json() to retrieve your JSON data at the server end:

from flask import Flask
from flask import request
import json

app = 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 sys
import json
import requests

conv = [{'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.

like image 106
Neil P Avatar answered Oct 20 '22 17:10

Neil P