Skip to main content
Paddy R perguntou em #Apex
I am trying to call one rest API using Httprequest (from salesforce using Apex code), but not able to figure how I can pass the body.  I would appreciate if anyone share the way to pass the below mentioned body in the HttpRequest.

here are the details:

End-point URL : https://api.boomi.com/api/rest/v1/AccountID/EnvironmentExtensions/XXXXXXXXXX

Body:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<bns:EnvironmentExtensions xmlns:bns="http://api.platform.boomi.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="XXXXXXXXXXXXXXXX" extensionGroupId="" environmentId="XXXXXXXXXXXXXXX">

    <bns:connections>

        <bns:connection name="SFDC connection" id="XXXX">

            <bns:field componentOverride="false" usesEncryption="false" encryptedValueSet="false" value="email@gmail.com" id="user"/>

            <bns:field componentOverride="false" usesEncryption="true" encryptedValueSet="true" id="password" value="password"/>

        </bns:connection>

    </bns:connections>

</bns:EnvironmentExtensions>

Apex code to call Rest API:

      String endPointURL = 'https://api.boomi.com/api/rest/v1/AccountID/EnvironmentExtensions/XXXXXXXXXX';

      

      String userName = 'XXXXXX';

      String password = 'XXXXXXX';

      String body= '';

      String query;

      query = '';

      Blob headerValue = Blob.valueOf(userName + ':' + password);

      String authorizationHeader = 'BASIC ' +

      EncodingUtil.base64Encode(headerValue);

      accs1 = new List<cAccount>();

      Httprequest request = new HttpRequest();

      Http http = new Http();

      request.setMethod('POST');

      request.setEndpoint(endPointURL);

      request.setHeader('Content-Type', 'application/json');

      // Header info with remote server user name and password

      request.setHeader('Authorization', authorizationHeader);

      request.setBody(query); 

            

      request.setTimeout(120000); 

      request.setBody(query);          

      //Making call to external REST API

      HttpResponse response = http.send(request);  

      System.debug('responseBody: '+response.getBody());
5 respostas
  1. 20 de mai. de 2016, 05:43
    In order to pass the Body,

    Do like this:

     

    String requestString = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <bns:EnvironmentExtensions xmlns:bns="http://api.platform.boomi.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="XXXXXXXXXXXXXXXX" extensionGroupId="" environmentId="XXXXXXXXXXXXXXX"> <bns:connections> <bns:connection name="SFDC connection" id="XXXX"> <bns:field componentOverride="false" usesEncryption="false" encryptedValueSet="false" value="email@gmail.com" id="user"/> <bns:field componentOverride="false" usesEncryption="true" encryptedValueSet="true" id="password" value="password"/> </bns:connection> </bns:connections> </bns:EnvironmentExtensions>';

    request.setBody(requestString);

    Kindly Mark as solved if this resolves your issue.

    Best Regards

    Veenesh