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.