19

I'm looking for a way to create an HttpRequest in Apex using a POST method. When I do it, I hit this error:

{
    "message": "HTTP Method 'POST' not allowed. Allowed are HEAD,GET,PATCH,DELETE",
    "errorCode": "METHOD_NOT_ALLOWED"
}

When I try to make a PATCH request (to update), there's an exception appearing in the developer console:

FATAL_ERROR System.CalloutException: Invalid HTTP method: PATCH

Just to give a quick background, I'm trying to use Salesforce's REST API from inside my Apex class to update an CurrencyType record. It's working outside Salesforce (using curl for example).

Thanks.

3 Answers 3

22

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

5
  • 3
    Wow, cool, this is working. It's a little bit weird thought that the Salesforce's API support PATCH HTTP method, but not their HttpRequest object in Apex. Thanks Andrew!
    – jpmonette
    Jun 27, 2013 at 14:13
  • 2
    Yes I was surprised at that as well! Your very welcome! :) Jun 27, 2013 at 14:14
  • I am trying with same piece of code to update ApexClass. But it says "INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY". But POST and GET to Create and Read the same class worked fine. Just not able to update the class using PATCH as explained above.Debug log looks like this: Request :"System.HttpRequest[Endpoint=c.ap1.visual.force.com/services/data/v28.0/tooling/sobjects/…, Method=POST]" , and Response = "[{"fields":[],"message":"insufficient access rights on cross-reference id","errorCode":"INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY"}]"
    – Soberano
    Jan 11, 2014 at 11:46
  • I found this recently, i didn't have time to investigate further though, it is strange since the docs state this object supports PATCH. I wondered if it was a doc error, and really only ApexClassMember can be updated, in which case you need the added overhead of a container etc. Jan 12, 2014 at 11:10
  • @AndrewFawcett now that you are an insider at Salesforce, could you put some light into this idea (id=0873A000000PST0QAO) for us? To be more specific, services like Microsoft Graph API only allow updates on some records using the PATCH verb. Meaning that it is currently impossible to do so with Apex! Mar 28, 2019 at 21:35
6

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)

1
0

PATCH Method Callouts are now available since Winter 21. req.setMethod('PATCH');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .