function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VinodBatraVinodBatra 

Bulk API 2.0 Patch Method is not working from Apex

Hello All, 

I am trying to close the created job (bulk api 2.0 job) which required PATCH request.

It works from workbench however when I am doing this using apex callouts it is not working It gives me Status Code 405 Status Method not allowed.

I tried the following workarounds but none worked.
1. req.setMethod('POST');
    req.setHeader('X-HTTP-Method-Override', 'PATCH');
2. req.setMethod('POST');
    url += '?_HTTPMethod=PATCH';
   req.setEndpoint(url);

If anyone knows any workaround for this let me know. 

Thanks In Advance.
Vinod
SwethaSwetha (Salesforce Developers) 
HI Vinod,
This seems to be specific to your implementation and not salesforce related as workbench patch request succeeds. Can you share your code snippet of 'PATCH'.

Related articles:
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_update_fields.htm
https://salesforce.stackexchange.com/questions/57215/how-can-i-make-a-patch-http-callout-from-apex
https://salesforce.stackexchange.com/questions/13294/patch-request-using-apex-httprequest
https://salesforce.stackexchange.com/questions/301043/updating-the-status-of-a-job-via-bulk-api-2-0

Thanks
VinodBatraVinodBatra
Hi Swetha,

Thanks for your reply.

Please find the following code snippet which I have tried:

1. In this, I am getting 405 - Method Not Allowed.
HTTP http = new HTTP();
HTTPResponse httpResponse = new HTTPResponse();
HTTPRequest httpReq = new HTTPRequest();
String strPatchRequest = '';

httpReq.setEndpoint('https://vinod-lwc-dev-ed.my.salesforce.com/services/data/v48.0/jobs/ingest/7502v00000O9R0xAAF/');
httpReq.setHeader('Authorization', 'Bearer ' + GetSessionIdController.getSessionId());
httpReq.setHeader('Content-Type', 'application/json;charset=UTF-8');
httpReq.setHeader('Accept', 'application/json');
httpReq.setHeader('X-HTTPS-Method-Override','PATCH');
httpReq.setMethod('POST');
strPatchRequest = '{'+
'"state" : "UploadComplete"'+
'}';
httpReq.setBody(strPatchRequest);
httpResponse = http.send(httpReq);
System.debug('Third Response -> '+httpResponse);

2. In this, I am getting 400 - Bad Request.
HTTP http = new HTTP();
HTTPResponse httpResponse = new HTTPResponse();
HTTPRequest httpReq = new HTTPRequest();
String strPatchRequest = '';

httpReq.setEndpoint('https://vinod-lwc-dev-ed.my.salesforce.com/services/data/v48.0/jobs/ingest/7502v00000O9R0xAAF?_HTTPMethod=PATCH');
httpReq.setHeader('Authorization', 'Bearer ' + GetSessionIdController.getSessionId());
httpReq.setHeader('Content-Type', 'application/json;charset=UTF-8');
httpReq.setHeader('Accept', 'application/json');
httpReq.setMethod('POST');
strPatchRequest = '{'+
'"state" : "UploadComplete"'+
'}';
httpReq.setBody(strPatchRequest);
httpResponse = http.send(httpReq);
System.debug('Third Response -> '+httpResponse);

Thanks,
Vinod