Posting json with httpclient.AsyncHTTPClient() http.fetch

2,543 views
Skip to first unread message

Johan Andersson

unread,
Jun 20, 2011, 5:23:43 AM6/20/11
to python-...@googlegroups.com
Hey.
Im trying to post json using this code:
post_args = str({"data": {"testar": "hejsan", "kind": "cola", "soft":
13.0}})
http = tornado.httpclient.AsyncHTTPClient()
http.fetch('https://api.domain.tld/oauth/v2/stuff',headers={"Content-Type":
"application/json"},body=post_args,callback=self.on_response)

but i get:
Traceback (most recent call last):
File
"/usr/local/lib/python2.6/dist-packages/tornado/simple_httpclient.py",
line 259, in cleanup
yield
File
"/usr/local/lib/python2.6/dist-packages/tornado/stack_context.py", line
183, in wrapped
callback(*args, **kwargs)
File
"/usr/local/lib/python2.6/dist-packages/tornado/simple_httpclient.py",
line 236, in _on_connect
assert self.request.body is None
AssertionError

i have tried using latest stable and git-head.
So, what is wrong or should i look at something else?

cheers

--
Johan Andersson

Ben Darnell

unread,
Jun 20, 2011, 12:01:16 PM6/20/11
to python-...@googlegroups.com
You need to add method="POST" to the fetch call.

-Ben

Johan Andersson

unread,
Jun 20, 2011, 12:36:06 PM6/20/11
to python-...@googlegroups.com
Oops. fail on me that i didnt see that.
thanks :)

-- 
Johan Andersson

Hsiaoming Young

unread,
Jul 1, 2011, 7:24:22 AM7/1/11
to python-...@googlegroups.com
Ben, why not just add method="POST" when body is not None.

--
Website: http://lepture.com
Twitter: @lepture

Ben Darnell

unread,
Jul 2, 2011, 3:15:11 PM7/2/11
to python-...@googlegroups.com
While GET with a body is not generally useful, it's not a nonsensical combination and it's not prohibited by the HTTP spec (also see the "multipart/form-data in a GET" thread where we're debating whether it should be allowed).  More importantly, there are semantic differences between GET and POST - GETs cannot have side effects (in a properly-written server).  I don't think it's a good idea to silently convert a method that can't have side effects into one that can just because the user requested a combination of options that suggest that's what they probably meant; the choice of HTTP method should be made explicitly.

-Ben

Carl S. Yestrau Jr.

unread,
Jul 2, 2011, 11:13:28 PM7/2/11
to python-...@googlegroups.com
+1 HTTP method be made explicitly

On Saturday, July 2, 2011, Ben Darnell <b...@bendarnell.com> wrote:
&gt; While GET with a body is not generally useful, it's not a


nonsensical combination and it's not prohibited by the HTTP spec (also

see the &quot;multipart/form-data in a GET&quot; thread where we're


debating whether it should be allowed).  More importantly, there are
semantic differences between GET and POST - GETs cannot have side
effects (in a properly-written server).  I don't think it's a good
idea to silently convert a method that can't have side effects into
one that can just because the user requested a combination of options
that suggest that's what they probably meant; the choice of HTTP
method should be made explicitly.

&gt;
&gt;
&gt; -Ben
&gt;
&gt; On Fri, Jul 1, 2011 at 4:24 AM, Hsiaoming Young
&lt;sophe...@gmail.com&gt; wrote:
&gt;
&gt;
&gt; Ben, why not just add method=&quot;POST&quot; when body is not None.
&gt;
&gt; On Tue, Jun 21, 2011 at 00:01, Ben Darnell
&lt;b...@bendarnell.com&gt; wrote:
&gt;&gt; You need to add method=&quot;POST&quot; to the fetch call.
&gt;&gt; -Ben
&gt;&gt;
&gt;&gt; On Mon, Jun 20, 2011 at 2:23 AM, Johan Andersson
&lt;j...@i19.se&gt; wrote:
&gt;&gt;&gt;
&gt;&gt;&gt; Hey.
&gt;&gt;&gt; Im trying to post json using this code:
&gt;&gt;&gt; post_args = str({&quot;data&quot;: {&quot;testar&quot;:
&quot;hejsan&quot;, &quot;kind&quot;: &quot;cola&quot;,
&quot;soft&quot;:
&gt;&gt;&gt; 13.0}})
&gt;&gt;&gt; http = tornado.httpclient.AsyncHTTPClient()
&gt;&gt;&gt;
&gt;&gt;&gt; http.fetch('https://api.domain.tld/oauth/v2/stuff',headers={&quot;Content-Type&quot;:
&gt;&gt;&gt; &quot;application/json&quot;},body=post_args,callback=self.on_response)
&gt;&gt;&gt;
&gt;&gt;&gt;  but i get:
&gt;&gt;&gt; Traceback (most recent call last):
&gt;&gt;&gt;  File
&gt;&gt;&gt; &quot;/usr/local/lib/python2.6/dist-packages/tornado/simple_httpclient.py&quot;,
line
&gt;&gt;&gt; 259, in cleanup
&gt;&gt;&gt;    yield
&gt;&gt;&gt;  File
&quot;/usr/local/lib/python2.6/dist-packages/tornado/stack_context.py&quot;,
&gt;&gt;&gt; line 183, in wrapped
&gt;&gt;&gt;    callback(*args, **kwargs)
&gt;&gt;&gt;  File
&gt;&gt;&gt; &quot;/usr/local/lib/python2.6/dist-packages/tornado/simple_httpclient.py&quot;,
line
&gt;&gt;&gt; 236, in _on_connect
&gt;&gt;&gt;    assert self.request.body is None
&gt;&gt;&gt; AssertionError
&gt;&gt;&gt;
&gt;&gt;&gt; i have tried using latest stable and git-head.
&gt;&gt;&gt; So, what is wrong or should i look at something else?
&gt;&gt;&gt;
&gt;&gt;&gt; cheers
&gt;&gt;&gt;
&gt;&gt;&gt; --
&gt;&gt;&gt; Johan Andersson
&gt;&gt;
&gt;&gt;
&gt;
&gt;
&gt;
&gt; --
&gt; Website: http://lepture.com
&gt; Twitter: @lepture
&gt;
&gt;
&gt;

Reply all
Reply to author
Forward
0 new messages