How to Make a PATCH HTTP Callout in Apex?

How to Make a PATCH HTTP Callout in Apex?

On May 19, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on How to Make a PATCH HTTP Callout in Apex?
How to Make a PATCH HTTP Callout in Apex

Question:

How can I make a PATCH HTTP callout from Apex? I discovered that the HttpRequest class in Apex does not directly support the PATCH method. When attempting to send a PATCH request, I receive a System.CalloutException: Invalid HTTP method: PATCH.

This is problematic because I need to call out to a third-party service, and the workaround available for Salesforce REST API requests (using RestRequest) is not applicable.

What approaches or workarounds can I use to send a PATCH request from Salesforce? Using external tools or avoiding Apex is not an option.

Answer:

While Apex does not natively support the PATCH method in the HttpRequest class, a common workaround is to use the X-HTTP-Method-Override header. Many servers, including those of third-party APIs, accept this header to indicate the desired HTTP method. You can set the request method to POST and include this header to specify PATCH.

Looking for expert Salesforce training in Bangalore? CRS Info Solutions offers industry-leading Salesforce courses designed around real-time project scenarios for practical, hands-on learning.

Here is an example:

String eventId = 'abcdefghijklmnop';
String user = 'user@gmail.com';
user = EncodingUtil.urlEncode(user, 'UTF-8');

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(
    'https://www.googleapis.com/calendar/v3/calendars/' + user + '/events/' + eventId
);
req.setBody('{ "Description" : "test description" }');
req.setHeader('Authorization', 'Bearer ' + authToken);
req.setHeader('Content-Type', 'application/json');
req.setHeader('X-HTTP-Method-Override', 'PATCH');
req.setMethod('POST');
HttpResponse res = h.send(req);

In this example, the following steps are taken:

  1. The X-HTTP-Method-Override header is set to PATCH.
  2. The HTTP method is set to POST, which Salesforce supports.
  3. The server interprets the header and processes the request as a PATCH.

If the third-party API does not support X-HTTP-Method-Override, an alternative approach is to use middleware or an external service to handle the PATCH request. The middleware can accept a POST request from Salesforce and then perform the PATCH request to the target service. However, this requires additional infrastructure and may not always be viable.

This workaround ensures compatibility with servers that honor the X-HTTP-Method-Override header, allowing you to perform PATCH operations from Salesforce.

Unlock Your Salesforce Career with Expert Training in Bangalore

CRS Info Solutions brings you top-notch Salesforce training in Bangalore, tailored to meet your career aspirations. Our industry-focused program combines in-depth knowledge with real-world project experience, covering key modules such as Admin, Developer, and AI.

With hands-on learning, detailed class notes, and structured certification preparation, you’ll gain the confidence to excel in your Salesforce training journey. Our expert trainers also provide interview coaching to help you land your dream job.Whether you’re a beginner or a professional, our comprehensive curriculum equips you with the skills needed to master Salesforce’s cloud-based CRM solutions and thrive in a competitive job market.

Don’t miss out—join our free demo session today and take the first step toward a successful Salesforce career!!!

Related Links: Complete list of String Class Methods Apex in Salesforce

Array methods in Salesforce Apex


Comments are closed.