PATCH request using Apex HttpRequest

Here is a working POST example...

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v28.0/sobjects/CurrencyType/');
    req.setMethod('POST');
    req.setBody('{ "IsoCode" : "ADP", "DecimalPlaces" : 2, "ConversionRate" : 1, "IsActive" : "true" }');
    req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
    req.setHeader('Content-Type', 'application/json');
    HttpResponse res = h.send(req);

Here is a working PATCH example..

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v28.0/sobjects/CurrencyType/01Lb0000000TWoP?_HttpMethod=PATCH');
    req.setBody('{ "DecimalPlaces" : 2 }');
    req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
    req.setHeader('Content-Type', 'application/json');
    req.setMethod('POST');
    HttpResponse res = h.send(req);

Note: The documentation for setMethod does not list 'PATCH' as a method, though it says 'for example', so its unclear if its saying its not supported or the documentation author just missed it out.

Fortunately you can override the Http method as a URL parameter as described here.

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


1) Please vote for the PATCH verb to be added here: https://success.salesforce.com/ideaView?id=0873A000000PST0QAO

2) In case the URL parameter override does not work, you can attempt using an HTTP Header to override the method:

req.setMethod('POST');
req.setHeader('X-HTTP-Method-Override', 'PATCH');

More info here: https://docs.oracle.com/cloud/february2016/servicecs_gs/CXSVC/Chunk1667882014.html

(Supported Operations > PATCH > HTTP Tunneling)