Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy wsgi.input if I want to process POST data more than once?

Tags:

python

wsgi

In WSGI, post data is consumed by reading the file-like object environ['wsgi.input']. If a second element in the stack also wants to read post data it may hang the program by reading when there's nothing more to read.

How should I copy the POST data so it can be processed multiple times?

like image 970
joeforker Avatar asked Nov 23 '09 14:11

joeforker


1 Answers

You could try putting a file-like replica of the stream back in the environment:

from cStringIO import StringIO

length = int(environ.get('CONTENT_LENGTH', '0'))
body = StringIO(environ['wsgi.input'].read(length))
environ['wsgi.input'] = body

Needing to do this is a bit of a smell, though. Ideally only one piece of code should be parsing the query string and post body, and delivering the results to other components.

like image 140
bobince Avatar answered Sep 28 '22 06:09

bobince