[SalesForce] Rest Api: “HTTP Method ‘PUT’ not allowed. Allowed are HEAD,GET,PATCH,DELETE”

I am trying to update a contact record using rest api but facing issue like
POST method is not allowed.

If I use PUT method. it is also not allowed

I have tried this solution How can I make a PATCH HTTP Callout from Apex? but didn't worked.

Any issue with code. any help

 id conid= ApexPages.currentPage().getParameters().get('conid');
        try{
            Boolean isSandbox = true;
            String uri          = 'https://' + (isSandbox ? 'test' : 'login') + '.salesforce.com/services/oauth2/token';
            String clientId     = EncodingUtil.urlEncode(clientId     ,'UTF-8');
            String clientSecret = EncodingUtil.urlEncode(clientSecret ,'UTF-8');
            String username     = EncodingUtil.urlEncode(USERNAME,'UTF-8');
            String password     = EncodingUtil.urlEncode(PASSWORD,'UTF-8');

            String body =   'grant_type=password&client_id=' + clientId + 
                            '&client_secret=' + clientSecret +
                            '&username=' + username + 
                            '&password=' + password; 

            HttpRequest hRqst   = new HttpRequest();
            hRqst.setEndpoint(uri);                     
            hRqst.setMethod('POST');                
            hRqst.setTimeout(6000); 
            hRqst.setBody(body);
            Http h = new Http();
            HttpResponse hRes = h.send(hRqst);

            Map<String,String> res = (Map<String,String>) JSON.deserialize(hRes.getBody(),Map<String,String>.class);

            hRqst  = new HttpRequest();
            system.debug('===hR11qst==='+res.get('instance_url')+'/services/data/v40.0/sobjects/Contact/'+conid);
            hRqst.setEndpoint(res.get('instance_url')+'/services/data/v40.0/sobjects/Contact/'+conid);                     
            // hRqst.setMethod('PUT');
            hRqst.setHeader('X-HTTP-Method-Override','PATCH');
            hRqst.setMethod('POST');
            hRqst.setHeader('Accept', 'application/json');
            hRqst.setHeader('Authorization','Bearer ' + res.get('access_token'));
            objCon.HasOptedOutOfEmail = true;
            String body1 = '{"id": '+conid+', HasOptedOutOfEmail":"true"}';
            hRqst.setBody(JSON.serialize(objCon));
            h = new Http();
            system.debug('===hRqst==='+hRqst);
            hRes = h.send(hRqst);
            system.debug('===body==='+hRes.getBody());
            // objCon.HasOptedOutOfEmail  = true;
            // update objCon;
        }
        catch(Exception ex){
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage());
            ApexPages.addMessage(myMsg);
        }

Best Answer

The correct method is mentioned in the Update a Record documentation. X-Http-Method-Override is not used by Salesforce. Instead, you need to add ?_HttpMethod=PATCH to have the Salesforce REST API interpret your request as a PATCH request.

If you use an HTTP library that doesn't allow overriding or setting an arbitrary HTTP method name, you can send a POST request and provide an override to the HTTP method via the query string parameter _HttpMethod.

hRqst.setEndpoint(
  res.get('instance_url')+
  '/services/data/v40.0/sobjects/Contact/'+conid+'?_HttpMethod=PATCH');                     
hRqst.setMethod('POST');

When you need to use PUT, you can also use this technique as well.

Related Topic