You need to sign in to do that
Don't have an account?
Using HttpRequest Object How To Set Header & Body Elements
<?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.
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:
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:
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:
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
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:
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:
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:
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.
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
Thanks for your help. Much appreciated!
Thanks for your help. Much appreciated!
Can u just explain what did u put in the header to correct ur mistake!!