AnsweredAssumed Answered

HttpRequest from Salesforce using Apex

Question asked by fpardeep455682 on May 20, 2016
Latest reply on Jun 2, 2016 by fpardeep455682

I am trying to call 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());

Outcomes