+ Start a Discussion
RedSalesRedSales 

Using HttpRequest Object How To Set Header & Body Elements

 

Hi, I'm new to webservices and struggling with understanding how to set some of the elements when using a HttpRequest object for a web service callout.  Here is a sample of the XML I need to call

 

<?xml version="1.0" encoding="utf-8"?>

<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">

  <soap12:Header>

    <ServiceAuthHeader xmlns="http:// dummyNameForBlogPost ">

      <User>string</User>

      <Password>string</Password>

    </ServiceAuthHeader>

  </soap12:Header>

  <soap12:Body>

    <create_record xmlns="http:/ dummyNameForBlogPost ">

      <packet>

        <ID>string</ID>

        <Address>string</Address>

        <Phone>string</Phone>

        <Email>string</Email>

      </packet>

    </create_record>

  </soap12:Body>

</soap12:Envelope>

<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">  <soap12:Header>    <ServiceAuthHeader xmlns="http://dummyNameForBlogPost ">      <User>string</User>      <Password>string</Password>    </ServiceAuthHeader>  </soap12:Header>  <soap12:Body>    <create_record xmlns="http://dummyNameForExpertsExc">      <packet>        <ID>string</ID>        <Address>string</Address>        <Phone>string</Phone>        <Email>string</Email>      </packet>    </create_record>  </soap12:Body></soap12:Envelope>

 

 

I tried to create my required code using HttpRequest/Response but wasn't 100% sure how to do so.  Here is my

sample code.

 

HttpRequest req = new HttpRequest();

req.setEndpoint('http://dummyNameForBlogPost.com/serviceTest.asmx ');

req.setMethod('POST');

req.setHeader('Content-Type', 'application/soap+xml');

req.setTimeout(60000);


String body="<?xml version="1.0" encoding="utf-8"?>

<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">

  <soap12:Header>

    <ServiceAuthHeader xmlns="http://dummyNameForBlogPost">

      <User>string</User>

      <Password>string</Password>

    </ServiceAuthHeader>

  </soap12:Header>

  <soap12:Body>

    <create_record xmlns="http://dummyNameForBlogPost">

      <packet>

        <ID>string</ID>

        <Address>string</Address>

        <Phone>string</Phone>

        <Email>string</Email>

      </packet>

    </create_record>

  </soap12:Body>

</soap12:Envelope>";



req.setBody(body);

Http http = new Http();  

try {        

  HTTPResponse res = http.send(req);     

  System.debug(res.toString());  

 

 } catch(System.CalloutException e) {  

  System.debug(e.toString());

 } 

 

 

I feel I should not put my Header element values in my "Body" though. Is this correct & if so how should I implement it? I presume I should set the header ServiceAuthHeader elements using req.setHeader().  

 

Thanks in advance for your help.


 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Don't let the terminology confuse you. The HTTP Request object uses a different notation than SOAP does. In an HttpRequest, there are two portions, a Header and a Body. Take a look at a sample POST method:

 

 

POST /survey.php HTTP/1.1
Host: www.contoso.com
Content-Length: 32
Content-Type: application/x-www-form-urlencoded

arg1=keafl&deaf=eiupoaa&arg2=yes

In an HTTP protocol, the red text above indicates the HTTP Header, while the blue text indicates the HTTP Body. The header is the first line of output until the first double-carriage-return characters (manifested as a "blank line" between the header and body; imagine yourself in Windows Notepad and pressing Enter twice to create a blank line). Anything located after this end-of-header marker is the body.

 

In SGML-type languages (HTML, XML, and so on), there is often a distinction between the header and body of the content. For example, in HTML, style sheets, the title of the web page, and JavaScript code typically lives in the header of the HTML file (as indicated by the head tag). Let's take a look at a HTML file:

 

 

 

<!DOCTYPE html>
<html>
  <head>
    <title>Under Construction</title>
  </head>
  <body>
    <h1>Under Construction</h1>
    <p>This page is currently under construction.</p>
  </body>
</html>

We can see that there is a header (in this case, the title of the webpage that should display) and the body (which contains text that will actually appear in the "client area" of the browser).

 

Now, when you view this same HTML file from the perspective of HTTP, you end up with this:

 

 

HTTP/1.1 200 OK
Date: Sat, 01 Jan 2000 07:00:00 GMT
Content-Length: 192
Connection: close
Content-Type: text/html

<!DOCTYPE html>
<html>
  <head>
    <title>Under Construction</title>
  </head>
  <body>
    <h1>Under Construction</h1>
    <p>This page is currently under construction.</p>
  </body>
</html>

As you can see, the header of the HTML is encapsulated in the body (or "payload") of the HTTP response from the server.

 

 

The point of this entire post was to show you that while you are placing the "header" within the "body", you're actually comparing apples to oranges: the SOAP header is part of the payload of the message, and therefore goes in the body of the HTTP request.

All Answers

sfdcfoxsfdcfox

Don't let the terminology confuse you. The HTTP Request object uses a different notation than SOAP does. In an HttpRequest, there are two portions, a Header and a Body. Take a look at a sample POST method:

 

 

POST /survey.php HTTP/1.1
Host: www.contoso.com
Content-Length: 32
Content-Type: application/x-www-form-urlencoded

arg1=keafl&deaf=eiupoaa&arg2=yes

In an HTTP protocol, the red text above indicates the HTTP Header, while the blue text indicates the HTTP Body. The header is the first line of output until the first double-carriage-return characters (manifested as a "blank line" between the header and body; imagine yourself in Windows Notepad and pressing Enter twice to create a blank line). Anything located after this end-of-header marker is the body.

 

In SGML-type languages (HTML, XML, and so on), there is often a distinction between the header and body of the content. For example, in HTML, style sheets, the title of the web page, and JavaScript code typically lives in the header of the HTML file (as indicated by the head tag). Let's take a look at a HTML file:

 

 

 

<!DOCTYPE html>
<html>
  <head>
    <title>Under Construction</title>
  </head>
  <body>
    <h1>Under Construction</h1>
    <p>This page is currently under construction.</p>
  </body>
</html>

We can see that there is a header (in this case, the title of the webpage that should display) and the body (which contains text that will actually appear in the "client area" of the browser).

 

Now, when you view this same HTML file from the perspective of HTTP, you end up with this:

 

 

HTTP/1.1 200 OK
Date: Sat, 01 Jan 2000 07:00:00 GMT
Content-Length: 192
Connection: close
Content-Type: text/html

<!DOCTYPE html>
<html>
  <head>
    <title>Under Construction</title>
  </head>
  <body>
    <h1>Under Construction</h1>
    <p>This page is currently under construction.</p>
  </body>
</html>

As you can see, the header of the HTML is encapsulated in the body (or "payload") of the HTTP response from the server.

 

 

The point of this entire post was to show you that while you are placing the "header" within the "body", you're actually comparing apples to oranges: the SOAP header is part of the payload of the message, and therefore goes in the body of the HTTP request.

This was selected as the best answer
Prajapati.LakhanPrajapati.Lakhan

Yes, sfdcfox is right SOAP header is part of the payload of the message, and therefore goes in the body of the HTTP request. But you need to recunstruct your HTTPRequest to set addtional parameters like 'SOAPAction' and Endpoint does not seem correct it looks like pointing to a WSDL if it is then try valid endpoint from WSDL  <Service> tag.

 

 

Thanks,

Lakhan

RedSalesRedSales

Thanks for your help.  Much appreciated!

RedSalesRedSales

Thanks for your help.  Much appreciated!

Mr_KhanMr_Khan

Can u just explain what did u put in the header to correct ur mistake!!