pythonpython-3.xwsgi

Accessing POST Data from WSGI


I can't seem to figure out how to access POST data using WSGI. I tried the example on the wsgi.org website and it didn't work. I'm using Python 3.0 right now. Please don't recommend a WSGI framework as that is not what I'm looking for.

I would like to figure out how to get it into a fieldstorage object.


Solution

  • Assuming you are trying to get just the POST data into a FieldStorage object:

    # env is the environment handed to you by the WSGI server.
    # I am removing the query string from the env before passing it to the
    # FieldStorage so we only have POST data in there.
    post_env = env.copy()
    post_env['QUERY_STRING'] = ''
    post = cgi.FieldStorage(
        fp=env['wsgi.input'],
        environ=post_env,
        keep_blank_values=True
    )