[Twisted-Python] http.Request and JSON
How do you read JSON content from a POST using http.Request. I peeked on args and it's empty. TIA.
On Tue, Jun 30, 2009 at 09:07:57AM +0100, Vincent Gulinao wrote:
How do you read JSON content from a POST using http.Request. I peeked on args and it's empty.
"args" is www-urlencoded only. If the content-type is something else it's not filled in. You want: request.content.read() IIRC
works :) On Tue, Jun 30, 2009 at 4:18 PM, Phil Mayers<p.mayers@imperial.ac.uk> wrote:
On Tue, Jun 30, 2009 at 09:07:57AM +0100, Vincent Gulinao wrote:
How do you read JSON content from a POST using http.Request. I peeked on args and it's empty.
"args" is www-urlencoded only. If the content-type is something else it's not filled in.
You want:
request.content.read()
IIRC
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Hi, On Tue, Jun 30, 2009 at 9:18 AM, Phil Mayers<p.mayers@imperial.ac.uk> wrote:
request.content.read()
To be safer, I do a request.content.getvalue() since content is a cStringIO object and you can never be sure if somewhere along the chain of processing someone hasn't done a .read and forgot to do a .seek(0,0). Cheers, Reza -- Reza Lotun +44 (0)7521 310 763 rlotun@gmail.com
On Tue, Jun 30, 2009 at 10:44 AM, Reza Lotun<rlotun@gmail.com> wrote:
To be safer, I do a request.content.getvalue() since content is a cStringIO object and you can never be sure if somewhere along the chain of processing someone hasn't done a .read and forgot to do a .seek(0,0).
You should probably do the seek/read instead, since I don't believe this is guaranteed to be a cStringIO; it might be an actual file on disk, or something else. -- mithrandi, i Ainil en-Balandor, a faer Ambar
Hi, On Tue, Jun 30, 2009 at 12:56 PM, Tristan Seligmann<mithrandi@mithrandi.net> wrote:
To be safer, I do a request.content.getvalue() since content is a cStringIO object and you can never be sure if somewhere along the chain of processing someone hasn't done a .read and forgot to do a .seek(0,0).
You should probably do the seek/read instead, since I don't believe this is guaranteed to be a cStringIO; it might be an actual file on disk, or something else.
If that's true then there really should be a .get_body() method on the request object. Reza -- Reza Lotun +44 (0)7521 310 763 rlotun@gmail.com
On Tue, 30 Jun 2009 13:12:05 +0100, Reza Lotun <rlotun@gmail.com> wrote:
Hi,
On Tue, Jun 30, 2009 at 12:56 PM, Tristan Seligmann<mithrandi@mithrandi.net> wrote:
To be safer, I do a request.content.getvalue() since content is a cStringIO object and you can never be sure if somewhere along the chain of processing someone hasn't done a .read and forgot to do a .seek(0,0).
You should probably do the seek/read instead, since I don't believe this is guaranteed to be a cStringIO; it might be an actual file on disk, or something else.
If that's true then there really should be a .get_body() method on the request object.
Why can't you seek and read? Also, this discussion would be more appropriate on the twisted-web mailing list. Jean-Paul
In my case at least it's cStringIO, and getvalue() sufficed. Sorry, subscribing to twisted-web now. Thanks everyone. On Tue, Jun 30, 2009 at 8:41 PM, Jean-Paul Calderone<exarkun@divmod.com> wrote:
On Tue, 30 Jun 2009 13:12:05 +0100, Reza Lotun <rlotun@gmail.com> wrote:
Hi,
On Tue, Jun 30, 2009 at 12:56 PM, Tristan Seligmann<mithrandi@mithrandi.net> wrote:
To be safer, I do a request.content.getvalue() since content is a cStringIO object and you can never be sure if somewhere along the chain of processing someone hasn't done a .read and forgot to do a .seek(0,0).
You should probably do the seek/read instead, since I don't believe this is guaranteed to be a cStringIO; it might be an actual file on disk, or something else.
If that's true then there really should be a .get_body() method on the request object.
Why can't you seek and read?
Also, this discussion would be more appropriate on the twisted-web mailing list.
Jean-Paul
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Tue, 2009-06-30 at 21:56 +0800, Vincent Gulinao wrote:
In my case at least it's cStringIO, and getvalue() sufficed.
Sorry, subscribing to twisted-web now.
Thanks everyone.
One last followup, for those not on twisted-web: DO NOT use getvalue(). The promised interface is file-like. And in fact, if the body is bigger than a certain size the object will not be a cStringIO and your code will break.
Tristan Seligmann <mithrandi <at> mithrandi.net> writes:
On Tue, Jun 30, 2009 at 10:44 AM, Reza Lotun<rlotun <at> gmail.com> wrote:
To be safer, I do a request.content.getvalue() since content is a cStringIO object and you can never be sure if somewhere along the chain of processing someone hasn't done a .read and forgot to do a .seek(0,0).
You should probably do the seek/read instead, since I don't believe this is guaranteed to be a cStringIO; it might be an actual file on disk, or something else.
Thank You!!! Every <stupid> example I've seen used "request.content.getvalue()" ...which fails miserably for me (possibly because I'm on 64bit windows - but cStringIO is available, so I'm not sure why it's not used). request.content.read() works wonderfully. The "getvalue" method looked familiar, but I couldn't recall where I'd seen it - only that *my* request.content doesn't have a getvalue method. It took quite a few searches before I stumbled on your comment here which gave me the correct solution.
On Jan 25, 2012, at 5:28 AM, Gerrat wrote:
Tristan Seligmann <mithrandi <at> mithrandi.net> writes:
On Tue, Jun 30, 2009 at 10:44 AM, Reza Lotun<rlotun <at> gmail.com> wrote:
To be safer, I do a request.content.getvalue() since content is a cStringIO object and you can never be sure if somewhere along the chain of processing someone hasn't done a .read and forgot to do a .seek(0,0).
You should probably do the seek/read instead, since I don't believe this is guaranteed to be a cStringIO; it might be an actual file on disk, or something else.
Thank You!!!
Every <stupid> example I've seen used "request.content.getvalue()" ...which fails miserably for me (possibly because I'm on 64bit windows - but cStringIO is available, so I'm not sure why it's not used). request.content.read() works wonderfully. The "getvalue" method looked familiar, but I couldn't recall where I'd seen it - only that *my* request.content doesn't have a getvalue method. It took quite a few searches before I stumbled on your comment here which gave me the correct solution.
Nothing to do with 64bit windows. twisted.web will switch to a file when the threshold of the upload goes above a certain size. request.content is a file-like object, not any particular implementation of that protocol. This really should be documented in http://twistedmatrix.com/documents/current/api/twisted.web.iweb.IRequest.htm... - it's a bug that it isn't. -glyph
participants (8)
-
Gerrat
-
Glyph Lefkowitz
-
Itamar Shtull-Trauring
-
Jean-Paul Calderone
-
Phil Mayers
-
Reza Lotun
-
Tristan Seligmann
-
Vincent Gulinao