• Now all data be ready for publish, We had made our blog alive now. Haha.



  • These days CCP GFW blocked all the IPs from US and when I switch my IP in google Cloud, the disk data cannot be restore again. I have to restart my blog totally, Now the website is still building…



Get the data received in a Flask request- Stack Overflow

Python PingBook 1 months ago (10-23) 12 0

Question

I want to be able to get the data sent to my Flask app. I’ve tried accessing request.data but it is an empty string. How do you access request data?

@app.route(‘/’, methods=[‘GET’, ‘POST’])
def parse_request():
data = request.data # data is empty
# need posted data here

The answer to this question led me to ask Get raw POST body in Python Flask regardless of Content-Type header next, which is about getting the raw data rather than the parsed data.

Answer

The docs describe the attributes available on the request. In most common cases request.data will be empty because it’s used as a fallback:

request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle.

request.args: the key/value pairs in the URL query string
request.form: the key/value pairs in the body, from a HTML post form, or JavaScript request that isn’t JSON encoded
request.files: the files in the body, which Flask keeps separate from form. HTML forms must use enctype=multipart/form-data or files will not be uploaded.
request.values: combined args and form, preferring args if keys overlap
request.json: parsed JSON data. The request must have the application/json content type, or use request.get_json(force=True) to ignore the content type.

All of these are MultiDict instances (except for json). You can access values using:

request.form[‘name’]: use indexing if you know the key exists
request.form.get(‘name’): use get if the key might not exist
request.form.getlist(‘name’): use getlist if the key is sent multiple times and you want a list of values. get only returns the first value.




Copyright from PingBook Blog, If not specified, they are original. This site uses BY-NC-SAProtocol authenticated.
For reprinting, please indicate the link of the original text:Get the data received in a Flask request- Stack Overflow
LIKE (0)
[1725641479@qq.com]
SHARE (0)
PingBook
Author:
We create, We sharing! Tag every value data your sharing
Submit comments
Cancel comments
emoji picture bold strikethrough center italic check in

Hi,you need to provide your name and email adress!

  • Name (Required)
  • Email (Required)
  • Website