jurasic
jurasic

Reputation: 1

HTTPConnection to make DELETE request: 505 response

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?

Upvotes: 0

Views: 1933

Answers (3)

Oleksandr S.
Oleksandr S.

Reputation: 2034

You can use urllib2:

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

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1124170

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))

Upvotes: 1

yotommy
yotommy

Reputation: 1496

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

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

Upvotes: 0

Related Questions