Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After doing req.stream.read(), is there a way to read the POST data again? #417

Open
dukedougal opened this issue Jan 24, 2015 · 6 comments

Comments

@dukedougal
Copy link

req.stream.seek(0) does not seem to work.

@tudborg
Copy link
Contributor

tudborg commented Feb 9, 2015

I'll try to answer this then :)

There is no way to read req.stream again. the req.stream is just the wsgi.input (although it might be wrapped in some extra stuff) for the request. You need to store it somewhere if you need to read it again.

However, as a hack, i think you could create a hook that replaces the stream with a StringIO object, that reads the entire stream the first time.

Note that if the content type is application/x-www-form-urlencoded the stream will have already been read, and the above aproach wouldn't work.

You could also extend Request and read the stream into a StringIO. Note however that if someone POST's a 4GB file, you will read all of that into memory, so you might want to stop reading at some number of bytes.

@kgriffs
Copy link
Member

kgriffs commented Feb 10, 2015

Generally, I'd like to avoid having Falcon itself automagically buffer the input stream, since as @tbug mentioned, there are different ways you would want to do it (or not do it at all), depending on your use case. You have to consider memory and performance overhead, security, latency, etc.

@dukedougal
Copy link
Author

That's fine, I can handle that, as long as Falcon has not already consumed the stream before I get a chance to read it..... :-)

@necaris
Copy link

necaris commented Apr 19, 2015

This should be handled by #493

@kgriffs
Copy link
Member

kgriffs commented May 5, 2016

As of Falcon 1.0.0, the framework will no longer automatically parse, by default, requests that have the content type "application/x-www-form-urlencoded". This allows the app to consume the stream itself, if desired.

Alternatively, the automatic parsing can be enabled, but a custom subclass of Request could be used to define a new __init__() that reads env['wsgi.input'] and then replaces it with a BytesIO instance.

@kgriffs kgriffs added this to the 1.1 milestone May 5, 2016
@kgriffs
Copy link
Member

kgriffs commented May 25, 2016

Combine with #471

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants