Post-Winter 21
As Daniel Ballinger mentioned in his new Answer below - Winter 21 will bring support for this at last! See https://releasenotes.docs.salesforce.com/en-us/winter21/release-notes/rn_apex_callouts_patch.htm
Pre-Winter 21
Update to this answer, there are 2 workarounds to the missing PATCH verb in HttpRequest
that may work in a few specific contexts:
- If you are calling TO Salesforce, FROM Salesforce (e.g. an integration between two orgs), you can append
?_HttpMethod=PATCH
to the endpoint. Note that this is not a feature of the HttpRequest
object, it doesn't do anything clever with this - rather this is a workaround that Salesforce has in place to allow callers to its REST APIs to do PATCH calls when the caller doesn't otherwise allow the PATCH verb (which ironically is a problem Salesforce itself suffers). That is to say, this will not help you if you're calling to a non-Salesforce system. See https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_update_fields.htm:
If you use an HTTP library that doesn't allow overriding or setting an arbitrary HTTP method name, you can send a POST request and provide an override to the HTTP method via the query string parameter _HttpMethod. In the PATCH example, you can replace the PostMethod line with one that doesn't use override:
PostMethod m = new PostMethod(url + "?_HttpMethod=PATCH");
- As noted by @zachelrath in their answer on this question (https://salesforce.stackexchange.com/a/77833/2429), setting the header
X-HTTP-Method-Override
is an option, e.g.
req.setMethod('POST');
req.setHeader('X-HTTP-Method-Override','PATCH');
However, this is a convention that some servers follow in order to support libraries that don't support certain HTTP verbs, rather than a guarantee of success. This DOES NOT work when calling into Salesforce, Salesforce ignores this header.
If neither of the above options work, it appears to not be possible using Apex alone, without some kind of middleware (e.g. Heroku proxy to turn certain POSTs into PATCHes, or [horror of horrors] a dummy visualforce page to run an XHR) - which you've indicated is not an option.
The reason for this looks like it might be because Salesforce behind the scenes is using JDK's HttpURLConnection
, which restricts the http verbs you can use to the HTTP/1.1 defaults. This bug report for your issue in a JDK/HttpUrlConnection context I found is marked as "Won't Fix" in OpenJDK - https://bugs.openjdk.java.net/browse/JDK-7016595. Note the description and error message match your issue.
This other StackExchange question indicates it's a problem across JDKs 6,7, and 8 (and I would presume earlier): https://stackoverflow.com/questions/17780693/ - same error phrasing still: Invalid HTTP method: PATCH