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

前端 未结 3 2036
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 14:58

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 han

3条回答
  •  失恋的感觉
    2020-12-20 15:33

    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.

提交回复
热议问题