Monday, March 9, 2015
Making PATCH requests from App Engine
PATCH
requests allow you to perform partial updates on many of our REST APIs and in most cases can save bandwidth.
If you have ever tried to do a PATCH request on an App Engine application, you probably realized that it is not possible and that the list of HTTP Methods allowed is whitelisted to the following methods only: GET
, POST
, HEAD
, PUT
and DELETE
. Trying to perform a PATCH
request raises the following Exception:
java.net.ProtocolException: PATCH
is not one of the supported http methods: [GET, POST, HEAD, PUT, DELETE]
There is a workaround to this. Most of our APIs support the X-HTTP-Method-Override
header. This header can be used in a POST
request to “fake” other HTTP methods. Simply set the value of the X-HTTP-Method-Override
header to the HTTP method you would like to actually perform.
For example, to make a PATCH
request to the Google Tasks API to update only the Notes
field of a particular task you could use the following HTTP request:
POST /tasks/v1/lists/@default/tasks/TASK_ID HTTP/1.1
Host: www.googleapis.com
X-HTTP-Method-Override: PATCH
Authorization: Bearer
Content-Type: application/json
Content-Length: 31
{“Notes” : “Patch is working!”}
Which would be equivalent to this HTTP Request, which is not supported on App Engine:
PATCH /tasks/v1/lists/@default/tasks/TASK_ID HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer
Content-Type: application/json
Content-Length: 31
{“Notes” : “Patch is working!”}
For instance, in an App Engine Java environment you could construct and execute this request this way:
URL url = new URL("https://www.googleapis.com/tasks/v1/" +
"lists/@default/tasks/" + TASK_ID);
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
request.addHeader(new HTTPHeader("X-HTTP-Method-Override", "PATCH"));
request.addHeader(new HTTPHeader("Authorization", "Bearer " +
ACCESS_TOKEN));
request.setPayload("{"Notes" : "Patch is working!"}".getBytes());
URLFetchService fetchService = URLFetchServiceFactory.getURLFetchService();
HTTPResponse response = fetchService.fetch(request);
This trick can also be used if your application server is behind a firewall, behind a proxy server or in any other environment where HTTP methods other than POST
might not be allowed. In that case you could use the X-HTTP-Method-Override
header the same way to workaround these limitations.
You may also use our Google APIs Client library for Java or our Google APIs Client library for Python, both of which have support for PATCH
requests and use the X-HTTP-Method-Override
header when appropriate.
Nicolas Garnier profile | twitter | events Nicolas joined Google’s Developer Relations in 2008. Since then hes worked on commerce oriented products such as Google Checkout and Google Base. Currently, he is working on Google Apps with a focus on the Google Calendar API, the Google Contacts API, and the Tasks API. Before joining Google, Nicolas worked at Airbus and at the French Space Agency where he built web applications for scientific researchers. |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.