HTTPConnection to make DELETE request: 505 response

1.8k Views Asked by At

Frustratingly I'm needing to develop something on Python 2.6.4, and need to send a delete request to a server that seems to only support http 1.1. Here is my code:

httpConnection = httplib.HTTPConnection("localhost:9080")
httpConnection.request('DELETE', remainderURL)
httpResponse = httpConnection.getresponse()

The response code I then get is: 505 (HTTP version not supported)

I've tested sending a delete request via Firefox's RESTClient to the same URL and that works.

I can't use urllib2 because it doesn't support the DELETE request. Is the HTTPConnection object http 1.0 only? Or am I doing something wrong?

3

There are 3 best solutions below

0
yotommy On

httplib uses HTTP/1.1 (see HTTPConnection.putRequest method documentation).

Check httpResponse.version to see what version the server is using.

2
Martijn Pieters On

The HTTPConnection class uses HTTP/1.1 throughout, and the 505 seems to indicate it's the server that cannot handle HTTP/1.1 requests.

However, if you need to make DELETE requests, why not use the Requests package instead? A DELETE is as simple as:

import requests

requests.delete(url)

That won't magically solve your HTTP version mismatch, but you can enable verbose logging to figure out what is going on:

import sys
requests.delete(url, config=dict(verbose=sys.stderr))
0
Oleksandr Shmyrko On

You can use urllib2:

req = urllib2.Request(query_url)
req.get_method = lambda: 'DELETE'   # creates the delete method
url = urllib2.urlopen(req)