0

I am trying to update a value on my Salesforce record and integrate the date in another system using the Rest API. I am unable to do this using the PATCH request workaround in apex.

Booking__c reservation = [Select id,name,end__c,Impala_id__c from Booking__c 
                              where id=:reservationId];
DateTime dt = reservation.End__c;
dt = dt.addDays(1);
Integer endTime= dt.getTime().intValue()/1000;

HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + api_key);//some api key value
req.setHeader('Content-Type', 'application/json');
//req.setHeader('X-HTTP-Method-Override','PATCH');

req.setEndpoint('https://api.getimpala.com/v2/hotel/'+hotel_id+'/booking/'
                    +reservation.Impala_id__c+'?_HttpMethod=PATCH');
req.setBody('{"end":'+endTime+'}');
req.setMethod('POST');
Http http = new Http();
HTTPResponse res2 = http.send(req);
system.debug('status is: '+res2.getStatus());
system.debug('status is: '+res2.getStatusCode());

I tried using both the options (1-using header and 2-appending in url). whereas in postman this is working fine which means endpoint is up and running. enter image description here

3
  • 1
    Please, try to set req.setMethod('POST); before req.setHeader('X-HTTP-Method-Override','PATCH');. That might be a case, because header worked fine for me for PATCH endpoints. Commented Nov 8, 2019 at 13:00
  • The version with params in link will not work for you, because it works only for requests from SF to another SF org. Commented Nov 8, 2019 at 13:02
  • Can you add the exact response from both the Apex callout and the POSTMAN callout to review?
    – pbattisson
    Commented Nov 8, 2019 at 14:16

1 Answer 1

1

HttpRequest doesn't support PATCH as the method. From the docs:

Possible values for the method type include:


DELETE
GET
HEAD
POST
PUT
TRACE

Setting X-HTTP-Method-Override header to PATCH is a workaround some services support if a client can't issue a PATCH request. Docs for getimpala.com APIs do not mention this option nor do they mention appending _HttpMethod=PATCH to the endpoint as a possibility.

Recommendation: send a POST request to a proxy that lives outside of Salesforce, then have the proxy rewrite POST to PATCH, send the PATCH request to getimpala.com, grab the response and send the response back to Salesforce. Lots of proxies can do this, e.g. here's how it works with HAProxy.

You must log in to answer this question.

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