Get the data received in a Flask request
In a Flask application, you can access the data received in a request using the request
object. Here is an example of how to retrieve the data from a POST request:
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['POST'])
def receive_data():
data = request.data
# Do something with the data
return "Data Received"
if __name__ == '__main__':
app.run()
Watch a video course
Python - The Practical Guide
In this example, the request.data
attribute is used to access the raw data sent in the request body. Additionally, you can access the data sent in the query string or as form data using request.args
and request.form
, respectively.
Also, If you want to access JSON data in the request, you can use request.get_json()
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['POST'])
def receive_data():
data = request.get_json()
# Do something with the data
return "Data Received"
if __name__ == '__main__':
app.run()
This will parse the JSON data in the request body and return it as a Python dictionary.