2

I have a web service with the following contract:

POST /Service/service.asmx HTTP/1.1
Host: xxx.xxx.xxx
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "xxx.xxx.xxx/Service/Method"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <Request xmlns="xxx.xxx.xxx/Service/">
      <transactiontype>string</transactiontype>
      <username>string</username>
      <password>string</password>
    </Request>
  </soap:Header>
  <soap:Body>
    <Method xmlns="xxx.xxx.xxx/Service/">
      <xml>xml</xml>
    </Method>
  </soap:Body>
</soap:Envelope>

And I am trying to call the service using jquery. This is my code:

$.ajax({
    url: serverUrl + 'Method',
    type: "POST",
    dataType: "xml",
    data: { xml: "xml" },
    beforeSend: function (req) {
        req.setRequestHeader('Header', '<Request xmlns="xxx.xxx.xxx/Service/">'
                            +'<transactiontype>4</transactiontype>'
                            +'<agencyName>name</agencyName>'
                            +'<username>user</username>'
                            +'<password>pass</password>'
                            +'</Request>');                    
    },
    success: function (data) {
        alert(data.text);
    },
    error: function (request, status, errorThrown) {
        alert(status);
    }
});

However, the header content is not passed to the web service? How would I go about passing the header credentials to my web service call?

Eran Egozi
  • 775
  • 1
  • 7
  • 18
Mike
  • 21
  • 1
  • 2
  • Please show how the header is defined in the service. Also, are you able to pass the header using a C# client program? – John Saunders Dec 27 '10 at 01:22
  • Are you sure that XML stuff is supposed to be a "header"? From the "contract" it looks just like the payload. Note: `soap:Header` (just an element in the XML) has *nothing* to do with HTTP headers. –  Dec 27 '10 at 01:27
  • string string string Is what I get from the WSDL. How would I check how the header is defined? I just want to know how would I pass in the user credentials to the soap-based web service – Mike Dec 27 '10 at 01:28
  • @Mike Read my comment again :-) `setRequestHeader` is at the HTTP-level. Not related to `soap:Header`. The HTTP-header you may need to specify is `SOAPAction`. This is only required for SOAP 1.x, IIRC, and can be omitted for SOAP 2.x (which makes it more browser-friendly). –  Dec 27 '10 at 01:29
  • @pst so then how would I pass the username and password to the web service call? – Mike Dec 27 '10 at 01:31
  • @Mike Please see http://stackoverflow.com/questions/124269/simplest-soap-example-using-javascript -- the first answer has an example. Look at what "headers" are set, and where. –  Dec 27 '10 at 01:32

1 Answers1

1

soap:Header is an XML element inside the XML/SOAP data "payload". This is different than an HTTP headers. In the contract, SOAPAction (along with Content-Length, etc) is an HTTP header.

XmlHttpRequest.setRequestHeader is used for specifying HTTP headers. It has nothing to do with anything inside the XML (directly).

The first answer at Simplest SOAP example should give an example of how to make a SOAP request. Note:

xmlhttp.setRequestHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
xmlhttp.setRequestHeader("Content-Type", "text/xml");
...
var xml = '<?xml version="1.0" encoding="utf-8"?>' +
    '<soap:Envelope...' + etc;
xmlhttp.send(xml)

It is the XML which contains soap:Envelope and the child elements soap:Header and soap:Body.

Happy coding.

Community
  • 1
  • 1