Python httplib and POST

I'm currently working on a piece of code written by someone else. It uses httplib for server requests. It contains all the data in the correct format - for example, message body, header values, etc.

The problem is that every time he tries to send POST requests, there is data - I see this on the client side, but nothing comes to the server. I read the library spec and the use seems to be correct.

Selected library calls are as follows:

 import httplib conn = httplib.HTTPConnection('monkeylabs.pl', 80) conn.connect() request = conn.putrequest('POST', '/api/snippet/') headers = {} headers['Content-Type'] = 'application/json' headers['User-Agent'] = 'Envjs/1.618 (SpyderMonkey; U; Linux x86_64 2.6.38-10-generic; pl_PL.utf8; rv:2.7.1) Resig/20070309 PilotFish/1.3.pre03' headers['Accept'] = '*/*' for k in headers: conn.putheader(k, headers[k]) conn.endheaders() conn.send('[{"id":"route"}]') resp = conn.getresponse() print resp.status print resp.reason print resp.read() conn.close() 

Is this some kind of known issue, or what? I am using Python 2.7. Not sure how to check httplib version.

Please do not offer to exchange httplib for something else unless it looks like something similar (httplib2 is possible). As I said, the code is not mine, and it comes in much larger quantities than what I just wrote above. Refactoring this will cause a serious problem. I'm interested in any reliable workarounds.

EDIT

Debug output:

 send: 'POST /api/snippet/ HTTP/1.1\r\nHost: monkeylabs.pl\r\nAccept-Encoding: identity\r\nContent-Type: application/json\r\nAccept: */*\r\nUser-Agent: Envjs/1.618 (SpyderMonkey; U; Linux x86_64 2.6.38-10-generic; pl_PL.utf8; rv:2.7.1) Resig/20070309 PilotFish/1.3.pre03\r\n\r\n[{"id":"route"}]' reply: 'HTTP/1.0 201 CREATED\r\n' header: Date: Fri, 10 Jun 2011 23:54:00 GMT header: Server: WSGIServer/0.1 Python/2.7.1+ header: Vary: Cookie header: Content-Type: application/json header: Content-Length: 0 201 CREATED 

Please note that the information after the answer actually speaks of the server’s response, and not of the request itself, which in this case is empty. The main reason is because the request object itself is empty, which I can observe when I receive the log:

 [11/Jun/2011 01:54:00] "POST /api/snippet/ HTTP/1.1" 201 0 

And these three lines:

 `` <QueryDict: {}> <QueryDict: {}> 

of:

 print '`%s`' % request.raw_post_data print request.GET print request.POST 

on the Django server. Therefore, it seems that he is trying to send the body, but does not send it at the end.

EDIT (2)

Well, I took a dump, and he really told me that in the message sent from the browser, there is an additional parameter called "Content-Length", which was omitted when the library was used regularly. Stupid me.

+7
source share
3 answers

try adding:

 conn.set_debuglevel(1) 

so that you can see what is really happening.

+5
source

The putrequest method putrequest not automatically add a Content Length header; you need to do this yourself or use the request method.

Add this to your code above the for loop:

 headers['Content-Length'] = "%d"%(len('[{"id":"route"}]')) 
+6
source

Have you checked the httplib documentation? httplib is the standard Python library, and Python is also very well versed in online documentation: http://docs.python.org/library/httplib.html

Example from this page:

 >>> import httplib, urllib >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> headers = {"Content-type": "application/x-www-form-urlencoded", ... "Accept": "text/plain"} >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80") >>> conn.request("POST", "/cgi-bin/query", params, headers) >>> response = conn.getresponse() >>> print response.status, response.reason 200 OK >>> data = response.read() >>> conn.close() 

Your example looks more efficient than this, and if you read the API documentation - for example, for putrequest - you will see that you use it incorrectly in your example. In particular, it automatically adds the default Accept header.

Try to make your code more like a functional example, and try to understand the calls used in your code and used in a working example.

0
source

All Articles