The objective of this post is to explain how to parse and use JSON data from a POST request in Flask.
Introduction
The objective of this post is to explain how to parse and use JSON data from a POST request in Flask, a micro web framework for Python.
You can check an introduction to Flask here. More posts on Flask are listed in the “Related posts” section.
The code
First of all, we need to import the Flask class from the flask module, so all the functionality we need becomes available. We will also import the request object from the flask module, which we will use later.
Then, we create an instance of the Flask class. In the constructor, we pass the name of the module of our application, using the __name__ global variable.
from flask import Flask
from flask import request
app = Flask(__name__)
We will assume that the client will be posting JSON data, so we will specify a route that only answers to HTTP POST requests. Any GET request to the same URL will be ignored.
To do so, we specify the POST keyword to the methods argument of the route decorator. You can read more on how to control the HTTP allowed methods in this previous post. You can check the code bellow, which only has the route decorator and the declaration of the handling function.
@app.route('/postjson', methods = ['POST'])
def postJsonHandler():
Note that, in the previous code, we specified that the server will be listening on the /postjson URL.
First, to confirm if the content is of type JSON, we check the is_json attribute of the request object. This attribute indicates if this request is JSON or not [1].
To get the posted JSON data, we just need to call the get_json method on the request object, which parses the incoming JSON request data and returns it [2] as a Python dictionary.
You can check the full code bellow, which already includes the calling of the run method on the app object, to start the server. Note that we specified port 8090 for our server to be listening on.
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/postjson', methods = ['POST'])
def postJsonHandler():
print (request.is_json)
content = request.get_json()
print (content)
return 'JSON posted'
app.run(host='0.0.0.0', port= 8090)
Testing the code
We can test this code by using Postman, which allows us to do POST requests very easily. After installing this Chrome extension, open it and, on the request tab, select the POST method from the dropdown.
On the URL, use 127.0.0.1:8090/postjson. So, we are using our machine’s loopback IP and the 8090 port we specified in the Python code. Then, click in the Body separator, so we can specify our JSON content.
All the relevant areas mentioned before are highlighted in figure 1.

On the Body separator, choose the radio button raw. A text editor should become available. On the dropdown next to the radio buttons, choose JSON (application/json) and then input valid JSON on the text editor, as indicated in figure 2.

You can use other JSON content, but bellow there is the one used in this example.
{
"device": "TemperatureSensor",
"value": "20",
"timestamp": "25/01/2017 10:10:05"
}
Finally, hit send and the POST request should be sent to the Flask server. You should get an output similar to the one in figure 3 if you are running the code on IDLE, the Python default IDE.

The “True” is the value of the is_json attribute, which indicates that the content was JSON. The string corresponds to our JSON content, sent from POSTMAN.
Note that we are printing the whole content from the dictionary that corresponds to the parsed JSON content. You can print each attribute of the JSON object by accessing the dictionary by the name of the attribute, as shown bellow.
print (content['device'])
print (content['value'])
print (content['timestamp'])
Related posts
- Python anywhere: Forcing HTTPS on Flask app
- Python anywhere: Deploying a Flask server on the cloud
- LinkIt Smart Duo: Running a Flask server
- Flask: Hello World
- Flask: Controlling HTTP methods allowed
References
[1] http://flask.pocoo.org/docs/0.12/api/#flask.Request.is_json
[2] http://flask.pocoo.org/docs/0.12/api/#flask.Request.get_json
Technical details
- Python version:3.6.0
- Flask library: 0.12
I might try this later 😎😎
Awesome 🙂 If you do, tell me if it works well for you
OK 😎😎
Awesome 🙂 If you do, tell me if it works well for you
Pingback: ESP8266: Posting JSON data to a Flask server on the cloud | techtutorialsx
Pingback: ESP8266: Posting JSON data to a Flask server on the cloud | techtutorialsx
Good article, works fine.
i have a question, is there a way to add the Basic-Authorisation credentials from postman and decode it with the flask code.
thanks.
Hi, thank you very much for the feedback 🙂
Unfortunately I’ve never tried it using Postman, but from this link it seems to be possible:
https://www.getpostman.com/docs/postman/sending_api_requests/authorization
Hope it helps,
Nuno Santos
Good article, works fine.
i have a question, is there a way to add the Basic-Authorisation credentials from postman and decode it with the flask code.
thanks.
Hi, thank you very much for the feedback 🙂
Unfortunately I’ve never tried it using Postman, but from this link it seems to be possible:
https://www.getpostman.com/docs/postman/sending_api_requests/authorization
Hope it helps,
Nuno Santos
It works!
Also the testing tool Postman you introduced is really useful! Thanks!
Hi! I’m glad the tutorial was useful for you 🙂
Best regards,
Nuno Santos
It works!
Also the testing tool Postman you introduced is really useful! Thanks!
Hi! I’m glad the tutorial was useful for you 🙂
Best regards,
Nuno Santos
Hi
i just wanted to know how this get_json parses the values, e.g. if value key in input json has a list of floating point numbers how it would handle.
Hi!
Unfortunately I don’t know what is the implementation under the hood, but you should be able to find it on the GitHub page of the module:
https://github.com/pallets/flask
However, I guess it should support what the JSON standard supports. But I think the fastest way is really doing a quick test and confirm how the function will behave with floating point numbers.
You can use for example postman to perform a request with some floats and check what is the result of the parsing.
Best regards,
Nuno Santos
Hi
i just wanted to know how this get_json parses the values, e.g. if value key in input json has a list of floating point numbers how it would handle.
Hi!
Unfortunately I don’t know what is the implementation under the hood, but you should be able to find it on the GitHub page of the module:
https://github.com/pallets/flask
However, I guess it should support what the JSON standard supports. But I think the fastest way is really doing a quick test and confirm how the function will behave with floating point numbers.
You can use for example postman to perform a request with some floats and check what is the result of the parsing.
Best regards,
Nuno Santos