21

Is there a way to make a request using the PATCH HTTP method in Python?

I tried using httplib, but it doesn't accept PATCH as method param.

jnns
  • 5,148
  • 4
  • 47
  • 74
Ricardo Augusto
  • 548
  • 1
  • 8
  • 18
  • 5
    Did you mean PUT? PUSH is not a valid request method for HTTP – dave Jul 28 '11 at 01:04
  • Check [here](http://stackoverflow.com/questions/111945/is-there-any-way-to-do-http-put-in-python), it uses PUT on that post, I guess you can use PUSH as a verb and wont give you trouble, still you should use PUT instead of PUSH – Hassek Jul 28 '11 at 01:28
  • Sorry, people, I typed the wrong method name, I edited the question o.O – Ricardo Augusto Jul 28 '11 at 12:42
  • 3
    Cat Plus Plus: yes, there is a PATCH method. dave: PUSH is "valid". There's a difference between "valid" and "registered". – Julian Reschke Jul 28 '11 at 12:57
  • Cat Plus Plus: here about PATCH method http://www.innoq.com/blog/st/2010/03/rfc_5789_patch_method_for_http.html It's ironic that Google support and use it in APIs but GAE urlfetch doesn't work with it. – Ricardo Augusto Jul 28 '11 at 13:36
  • salesforce.com REST API requires use of the PATCH method for updating objects, so I am looking for this too, so I can update objects from Google App Engine http://www.salesforce.com/us/developer/docs/api_rest/index.htm – Alpha Codemonkey Aug 10 '11 at 13:20

4 Answers4

24

With Requests, making PATCH requests is very simple:

import requests

r = requests.patch('http://httpbin.org/patch')
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Kenneth Reitz
  • 8,559
  • 4
  • 31
  • 34
16

Seems to work in 2.7.1 as well.

>>> import urllib2
>>> request = urllib2.Request('http://google.com')
>>> request.get_method = lambda: 'PATCH'
>>> resp = urllib2.urlopen(request)
Traceback (most recent call last):
 ...
urllib2.HTTPError: HTTP Error 405: Method Not Allowed
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Travis Jensen
  • 5,362
  • 3
  • 36
  • 40
  • 1
    Great information. This is a simple little hack to make urllib2 use PATCH instead of POST. I don't know why PATCH hasn't been implemented as an option yet. – Corey O. Sep 08 '12 at 17:24
4

I tried this in Python 3, and it seemed to work (but I don't have a server handy that supports the PATCH request type):

>>> import http.client
>>> c = http.client.HTTPConnection("www.google.com")
>>> r = c.request("PATCH", "/index.html")
>>> print(r.status, r.reason)
405 Method Not Allowed

I'm assuming that the HTTP 405 is coming from the server and that it is "not allowed".

By the way, thanks for showing me the cool PATCH method in HTTP.

Community
  • 1
  • 1
Chris
  • 3,000
  • 26
  • 43
  • Thanks for the answer, I will try that later and mark as accepted. GitHub API accepted POST instead of PATCH, but I will give that a try and keep this for future. – Ricardo Augusto Aug 13 '11 at 21:13
2

It is incredibly simple with httplib2:

import httplib2

http = httplib2.Http()
http.request("http://www.google.com", "PATCH", <patch content>)

It supports Python 2.3 or later (including 3.x) and works beautifully!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Andaris
  • 635
  • 6
  • 15