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
Sandrine94748Sandrine94748 

Call webservice from Salesforce

Hello,

I want to call a webservice with a POST method.

I have a webservice link, JSON file a,d i know that it accepts post method.

I want to know how can i call this service from Salesforce

thank you for suggestions

Best Answer chosen by Sandrine94748
EldonEldon
Hi,

Please follow this sample code for a REST http callout,
 
tp http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('YOUR WEBSERVICE LINK');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody('{"name":"Test Name"}'); //REPLACE WITH YOUR JSON FILE
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 201) {
    System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
} else {
    System.debug(response.getBody());
}

Regards