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.
Best Answer
HttpRequest doesn't support PATCH as the method. From the docs:
Setting
X-HTTP-Method-Override
header toPATCH
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.