Naveera A.
—How can you access the data that has been sent to a Flask app through a POST request?
A user can submit various types of data using a POST request. Depending on the type of data, Flask uses different ways to extract and parse the data before storing it in the global request object.
We’re going to take a look at two of the most common types of data. You can read more about how Flask handles other kinds of data on the official documentation.
When a user fills out a form on the frontend of a web app and hits submit, the browser sends that data to our app. The data is submitted using a POST request with the Content-Type header set to multipart/form-data
.
When Flask receives form data, it parses and stores it in the form
property of the request object. The form
property is an ImmutableMultiDict
, which implements all standard dictionary methods.
Let’s say a user submits a form with two input fields: name and age. We can extract form data, like so:
from flask import Flask, request # ... @app.route('/form_example', methods=['POST']) def handle_form(): print(request.form.get('name')) print(request.form.get('age')) return request.form
The most commonly used format of the incoming data is JSON. Flask stores the JSON data in the json
property of the request object. We can access the data like so:
from flask import Flask, request # ... @app.route('/json_example', methods=['POST']) def handle_json(): data = request.json print(data.get('name')) print(data.get('age')) return data
An important point to remember is that the json
will only contain the data if the Content-Type
header is set to application/json
.
We can use the headers
attribute of the request object to check the Content-Type
, like so:
from flask import Flask, request # ... @app.route('/json_example', methods=['POST']) def handle_json(): content_type = request.headers.get('Content-Type') if (content_type == 'application/json'): ... else: return "Content type is not supported."
A better way to check is to use the is_json
property of the request object:
from flask import Flask, request # ... @app.route('/json_example', methods=['POST']) def handle_json(): if request.is_json: ... else: ...
If the Content-Type
header is not set correctly, Flask will store the data as a string (even if it is JSON) in the data
property of the request object.
We can use Flask’s json
module to covert the string back to JSON, like so:
from flask import Flask, request, json # ... @app.route('/string_example', methods=['POST']) def handle_non_json(): data = json.loads(request.data) return data
The good thing about using the json
module is that it can extract the data correctly even when it receives a request with Content-Type
header set to application/json
.
Either of the following two requests will result in the same response:
$ curl -X POST -H "Content-type: application/json" -d "{\"name\" : \"John\", \"age\" : \"5\"}" "localhost:5000/string_example"
$ curl -X POST -H "Content-type: text/plain" -d "{\"name\" : \"John\", \"age\" : \"5\"}" "localhost:5000/string_example"
It is best to use this solution in situations where we don’t know which header will be sent to our app.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.