Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask: Getting request parameters based on Content-Type header

Tags:

python

rest

flask

What is the proper way to deal with the request body depending on the Content-Type header of the request?

I need to implement a RESTful service that supports XML, JSON and form encoded request parameters, but I can't seem to find a clean way of extracting the request parameters.

Is this something that I should use a middleware for? Do I need to extend the Request object?

I haven't found any packages that do this, and it seems like a pretty common task for creating RESTful services in flask.

like image 928
partkyle Avatar asked Jul 18 '26 04:07

partkyle


1 Answers

You probably meant Accept header, as Content-Type is used for response. There are three choices here:

  1. Build it youself as described in Handling Accept Headers snippet.
  2. Use Flask-RESTful extension (consult Content Negotiation part in the docs).
  3. Use Flask-Pushrod extension which is specifically built to handle this case.
like image 193
Audrius Kažukauskas Avatar answered Jul 22 '26 07:07

Audrius Kažukauskas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!