Search code examples
pythonflaskpostconnexionx-www-form-urlencoded

How to get raw request payload in Flask/Connexion?


I need to get the body of a POST request with Content-Type: application/x-www-form-urlencoded in a Connexion API handler. The accepted answer here suggests to use request.get_data(), however this always returns just b'' for me. My guess is that Connexion parses the data before request.get_data() gets called and that is causing the issue.

How do I get the raw request payload in this case? If there's no way to get it directly, how do I properly reconstruct it from request.form (which seems to contain the parsed data)?


Solution

  • I'm not sure if there's an easier way, but this seems to do the trick:

    import urllib.parse
    
    form_data = request.form
    request_data = '&'.join([k + '=' + urllib.parse.quote_plus(v) for k, v in form_data.items()])