+ Start a Discussion
SFDC developer55SFDC developer55 

Httprequest using Apex code

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());
Best Answer chosen by SFDC developer55
Veenesh VikramVeenesh Vikram
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

All Answers

Veenesh VikramVeenesh Vikram
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
This was selected as the best answer
SFDC developer55SFDC developer55
Hello Veenesh,

Thank you for your reply, but unfortunaltely that does not work..

Regards.
Paddy
Veenesh VikramVeenesh Vikram
Any error you are getting in particular?
SFDC developer55SFDC developer55
I am getting the below error:
<head> <title>Status page</title> </head> <body> <h3>The server encountered an unexpected condition which prevented it from fulfilling the request</h3><p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1">here</a>.<br/> Please continue your visit at our <a href="/">home page</a>. </p> </body> </html>
SFDC developer55SFDC developer55
Hello Veenesh,

Another quick question- 

In the same requeststring, I have to pass one value from the post form data.

For e.g: I have a user name text box and I need to pass that user name value on form submission in the below request string:

String abc = post data from form;

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>';


What i Need?
To pass abc string value in the body.

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= abc id="user"/> <bns:field componentOverride="false" usesEncryption="true" encryptedValueSet="true" id="password" value="password"/> </bns:connection> </bns:connections> </bns:EnvironmentExtensions>';


i would appreciate your help!