Difference between SOAP & HTTPRequest Headers?

Hello,

I have a webservice I need to call which contains a <soap12:Header> element within its XML.

<soap12:Header>
    <ServiceAuthHeader xmlns="http://dummyNameForExpertsExc">
      <User>string</User>
      <Password>string</Password>
    </ServiceAuthHeader>
  </soap12:Header>

In calling the webservice I will be doing so using the HttpRequest and HttpResponse apex classes.

the HttpRequest class has a method called setHeader.

In examples I have seen it is used to set attributes such as content type in the following

HttpRequest req = new HttpRequest();
req.setEndpoint('http://dummyNameForBlogPost.com/serviceTest.asmx ');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/soap+xml');
req.setTimeout(60000);

There is a setBody method for HttpRequest also.

Should my SOAP Header actually go with the rest of my XML in  the setBody method?
speedygonzalezAsked:
Who is Participating?
 
Holger101497Connect With a Mentor Commented:
Yes, the SOAP header is part of the "payload". Both soap:Header and soap:Body are wrapped in an element called soap:Envelope. You can find a very quick example at http://en.wikipedia.org/wiki/SOAP#Example_message

 
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
  </soap:Header>
  <soap:Body>
    <m:GetStockPrice xmlns:m="http://www.example.org/stock">
      <m:StockName>IBM</m:StockName>
    </m:GetStockPrice>
  </soap:Body>
</soap:Envelope>

Open in new window


The "HttpRequest" knows nothing about soap-specific features and the setHeader()-method will simply set the HTTP-HEADERS. (to make this a bit more confusing, you'll probably have to set an http-header called soap-action :-))

Hope that helps, let me know if you need more info.

0
 
speedygonzalezAuthor Commented:
Sorry accidently saved before finishing my question.

My question is

Should my SOAP Header actually go with the rest of my XML in  the setBody method? or should it be set in the HTTPRequest.setHeader() method?

This has really confused me.

Thanks.
0
 
speedygonzalezAuthor Commented:
Thanks. This answer is very useful!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.