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
Kiran Kumar PunuruKiran Kumar Punuru 

Issue with Patch Method in Http Rest Callouts(Rest Integration)

Hi All,

I have issue to update a record in another application from salesforce i am using the below code to update the record but looks like there is an issue becuase of that i am getting the below error:

Does someone tell me is there anything went wrong on my code with respect to body which i am using to update?

22:21:46:468 USER_DEBUG [188]|DEBUG|Response body{"ok":false,"errors":[{"code":"invalid_api_key","message":"Invalid token: bad hash"}]}

global static void updateRecipients(){
      
       string endPoint = 'https://api.paymentrails.com/v1/recipients/R-2nVbDmuyF7ykgd57eY1VQS';
       string reqpath = '/v1/recipients/R-2nVbDmuyF7ykgd57eY1VQS';
       string API_KEY = 'xyztest';
       string apiSecret = 'xyztest';
  
        string body = '{"lastName":"punuru"}';
    
        
      // Get the timestamp
      Datetime dt = Datetime.now();  
      Long l = dt.getTime();
      system.debug(l);
      Long epochDate = dt.getTime()/1000;
      Integer timeStamp = epochDate.intValue();  
      system.debug('@@@timeStamp'+timeStamp);
      string authoriation = 'prsign'+' '+API_KEY+':'+SecurityManagerController.generateAuthorization('PATCH',reqpath,apiSecret,timeStamp,body);
      system.debug('@@@authoriation'+authoriation);
     
     //Create Request
      HttpRequest req = new HttpRequest();
      req.setMethod('PUT');
      req.setHeader('X-HTTP-Method-Override','PATCH');
      req.setHeader('content-type', 'application/json');
      req.setHeader('Authorization', authoriation);
      req.setHeader('X-PR-Timestamp', timeStamp + '');
      req.setEndpoint(endPoint);
      req.setBody(body);
      Http http = new Http();
      HttpResponse res = http.send(req);
      system.debug('Response body'  +res.getBody());
      system.debug('STATUS: '+ res.getStatus());
      system.debug('STATUS_CODE:'+ res.getStatusCode());
    }

SecurityManagerController
public with sharing class SecurityManagerController {

  public static final String MACENCRIPTION = 'HmacSHA256';
 

  public static String generateAuthorization(String method,String requestPath,String secretKey,integer timestamp,String body){
    
         String signatureData = +timestamp + '\n' + method + '\n' + requestPath + '\n' + body + '\n' ;
        
        
        System.debug('@@@signatureData'+signatureData);
         Blob mac        = crypto.generateMac('HmacSHA256', Blob.valueOf(signatureData), Blob.valueOf(secretKey));
         system.debug('@@@data'+mac);
        // Blob hash = Crypto.generateDigest('SHA-256', mac);
        // Boolean verified = Crypto.verifyHMac('HmacSHA256', Blob.valueOf(signatureData), Blob.valueOf(secretKey), mac);
         System.debug('@@@verified'+mac);
         String signature = EncodingUtil.convertToHex(mac);
         System.debug('@@@docBody'+signature);
         return signature;
  }
 }

Thanks in Advance!!

Kiran Kumar PunuruKiran Kumar Punuru
I have also tried in the below ways as well:
1.
req.setMethod('POST');
req.setHeader('X-HTTP-Method-Override', 'PATCH');

2. Adding the below in the end point and using below method:

string endPoint = 'https://api.paymentrails.com/v1/recipients/R-2nVbDmuyF7ykgd57eY1VQS?_HttpMethod=PATCH';
req.setMethod('POST');

Reference : https://salesforce.stackexchange.com/questions/13294/patch-request-using-apex-httprequest