+ Start a Discussion
Jitendra RawatJitendra Rawat 

Httprequest.setMethod() not support "PATCH" method.

I have to send outobund http request by using PATCH method, but salesforce httprequest.setMethod(), not support PATCH method, So please help me how can I achive this?
Best Answer chosen by Jitendra Rawat
Grazitti TeamGrazitti Team
Hi Jitendra,

Please use POST or PUT method for PATCH request and append parameter ?_HttpMethod=PATCH in the endpoint as given in the following discussion-
http://salesforce.stackexchange.com/questions/13294/patch-request-using-apex-httprequest

Please mark this as Best Answer if it helps you.

Regards,
Grazitti Team
Web: www.grazitti.com
Email: sfdc@grazitti.com

All Answers

Grazitti TeamGrazitti Team
Hi Jitendra,

Please use POST or PUT method for PATCH request and append parameter ?_HttpMethod=PATCH in the endpoint as given in the following discussion-
http://salesforce.stackexchange.com/questions/13294/patch-request-using-apex-httprequest

Please mark this as Best Answer if it helps you.

Regards,
Grazitti Team
Web: www.grazitti.com
Email: sfdc@grazitti.com
This was selected as the best answer
sandip87sandip87
Hi, 

I stuck across the same issue and unfortunately not able to make 'PATCH' call with this solution by appending the ?_HttpMethod=PATCH parameter. Below is my code to call Microsoft Dynamics web API. 
 
HttpRequest req1 = new HttpRequest();
string token1 = GetToken(); // will give the access token.
String endPoint = 'https://******.api.crm.dynamics.com/api/data/v8.2/new_objName(e80dc6e7-55e1-e611-80f8- c4346bac4958)?_HttpMethod=PATCH';                
req1.setMethod('POST');    
req1.setBody('{"new_contractor":"Test Name 1"}');
 
req1.setHeader('Accept', 'application/json');
req1.setHeader('content-type', 'application/json');
req1.setHeader('OData-MaxVersion', '4.0');
req1.setHeader('OData-Version', '4.0');    
req1.setHeader('Authorization', token1);
req1.setHeader('charset', 'utf-8');
req1.setEndpoint(endPoint);            
Http client1 = new Http();     
HttpResponse res1 = client1.send(req1);

I'm not able to update the record and always have the 'Method Not Allowed' error. Below is the debug line of response.  
CALLOUT_RESPONSE|[104]|System.HttpResponse[Status=Method Not Allowed, StatusCode=405]


Also, even I'm passing the parameter 'PATCH' by appending the endpoint URL, the callout request is still showing the call method as 'POST' (Method=POST).
 
CALLOUT_REQUEST|[104]|System.HttpRequest[Endpoint=https://******.api.crm.dynamics.com/api/data/v8.2/new_objName(e80dc6e7-55e1-e611-80f8-c4346bac4958)?_HttpMethod=PATCH, Method=POST]

I've also tried to override the method by setting the header parameter as below without any luck. 
req1.setHeader('X-HTTP-Method-Override','PATCH');

It would be a great help if anyone suggest me to make successful PATCH call. 

Thanks, 
Sandip
Jitendra RawatJitendra Rawat
Hi Sandip,

I am able to process the same request but not getting the method not supported error. This is the reposne I am getting :
User-added image
 
sandip87sandip87
Hi Jitendra, 

Thanks for your reply. Can you please guide me if missed something? Have you found this method working anytime?

Thanks, 
Sandip
Natarajan _Periyasamy__cNatarajan _Periyasamy__c
Hi Sandip,

I'm getting the same error, did you get the solution for the above issue you had? Could you please help me?

Regards,
Natarajan