How To Get The Request Body Bytes In Flask?
Solution 1:
You can get the non-form-related data by calling request.get_data()
You can get the parsed form data by accessing request.form
and request.files
.
However, the order in which you access these two will change what is returned from get_data
. If you call it first, it will contain the full request body, including the raw form data. If you call it second, it will typically be empty, and form
will be populated. If you want consistent behavior, call request.get_data(parse_form_data=True)
.
You can get the body parsed as JSON by using request.get_json()
, but this does not happen automatically like your question suggests.
See the docs on dealing with request data for more information.
Solution 2:
To stream the data rather than reading it all at once, access request.stream
.
Solution 3:
If you want the data as a string instead of bytes, use request.get_data(as_text=True)
. This will only work if the body is actually text, not binary, data.
Solution 4:
If the data is JSON, use request.get_json()
to parse it.
Post a Comment for "How To Get The Request Body Bytes In Flask?"