Difference between SOAP & HTTPRequest Headers?


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

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

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.setHeader('Content-Type', 'application/soap+xml');

There is a setBody method for HttpRequest also.

Should my SOAP Header actually go with the rest of my XML in  the setBody method?
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">
    <m:GetStockPrice xmlns:m="http://www.example.org/stock">

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.

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.

speedygonzalezAuthor Commented:
Thanks. This answer is very useful!
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.