Adding a Custom header to your Web Service (WSDL)

Adding a Custom header to your Web Service (WSDL)

It’s very common that you have to define a WSDL and want it to have custom header.

I have been using WSDL’s with custom headers for some time and thought I will post this sample WSDL for all of you who want to define a SOAP 1.1 WSDL with a custom message header.

Below is a Sample SOAP 1.1 WSDL with custom headers.

LocationFinderService.wsdl

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”no”?>

<wsdl:definitions xmlns:soap=”http://schemas.xmlsoap.org/wsdl/soap/”

xmlns:tns=”http://www.nysolutionsltd.com/wsdl/utilityservices/addressfinderservice/LocationFinderServicev1.0/”

xmlns:wsdl=”http://schemas.xmlsoap.org/wsdl/”

xmlns:xsd=”http://www.w3.org/2001/XMLSchema”

name=”LocationFinderService”

xmlns:header=”http://www.nysolutionsltd.com/wsdlheaders/Headerv1.0″

targetNamespace=”http://www.nysolutionsltd.com/wsdl/utilityservices/addressfinderservice/LocationFinderServicev1.0/”

xmlns:xsd1=”http://www.nysolutionsltd.com/wsdlmessages/utilityservices/addressfinderservice/FindGeocodesForLocationv1.0″>

<wsdl:types>

<xsd:schema

targetNamespace=”http://www.nysolutionsltd.com/wsdl/utilityservices/addressfinderservice/LocationFinderServicev1.0/”>

</xsd:schema>

<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>

<xsd:import

namespace=”http://www.nysolutionsltd.com/wsdlmessages/utilityservices/addressfinderservice/FindGeocodesForLocationv1.0″

schemaLocation=”../../../../../ServiceSchemas/internal/wsdlmessages/utilityservices/addressfinderservice/FindGeocodesForLocation.xsd”>

</xsd:import>

<xsd:import

schemaLocation=”../../../../../ServiceSchemas/internal/wsdlheaders/HeaderSchema.xsd”

namespace=”http://www.nysolutionsltd.com/wsdlheaders/Headerv1.0″ />

</xsd:schema>

</wsdl:types>

<wsdl:message name=”MessageHeaderType”>

<wsdl:documentation>

The Messages used in the operations of this service are

defined here. The element attribute refers to the various

types defined in the schema.

</wsdl:documentation>

<wsdl:part name=”MessageHeaderType”

element=”header:MessageHeaderType” />

</wsdl:message>

<wsdl:message name=”findGeocodesForLocationRequest”>

<wsdl:part element=”xsd1:findGeocodesForLocationRequest”

name=”parameters” />

</wsdl:message>

<wsdl:message name=”findGeocodesForLocationResponse”>

<wsdl:part element=”xsd1:findGeocodesForLocationResponse”

name=”parameters” />

</wsdl:message>

<wsdl:portType name=”LocationFinderService”>

<wsdl:operation name=”findGeocodesForLocation”>

<wsdl:input message=”tns:findGeocodesForLocationRequest” />

<wsdl:output message=”tns:findGeocodesForLocationResponse” />

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name=”LocationFinderServiceSOAP”

type=”tns:LocationFinderService”>

<soap:binding style=”document”

transport=”http://schemas.xmlsoap.org/soap/http” />

<wsdl:operation name=”findGeocodesForLocation”>

<soap:operation

soapAction=”http://www.nysolutionsltd.com/wsdl/utilityservices/addressfinderservice/LocationFinderServicev1.0/findGeocodesForLocation” />

<wsdl:input>

<soap:header message=”tns:MessageHeaderType”

part=”MessageHeaderType” use=”literal” />

<soap:body use=”literal” />

</wsdl:input>

<wsdl:output>

<soap:body use=”literal” />

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name=”LocationFinderService”>

<wsdl:port binding=”tns:LocationFinderServiceSOAP”

name=”LocationFinderServiceSOAP”>

<soap:address location=”http://www.nysolutionsltd.com/” />

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

For Custom headers

Things to note are to define the custom header type:

<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>

<xsd:import

namespace=”http://www.nysolutionsltd.com/wsdlmessages/utilityservices/addressfinderservice/FindGeocodesForLocationv1.0″

schemaLocation=”../../../../../ServiceSchemas/internal/wsdlmessages/utilityservices/addressfinderservice/FindGeocodesForLocation.xsd”>

</xsd:import>

<xsd:import

schemaLocation=”../../../../../ServiceSchemas/internal/wsdlheaders/HeaderSchema.xsd”

namespace=”http://www.nysolutionsltd.com/wsdlheaders/Headerv1.0″ />

</xsd:schema>

</wsdl:types>

<wsdl:message name=”MessageHeaderType”>

<wsdl:documentation>

The Messages used in the operations of this service are

defined here. The element attribute refers to the various

types defined in the schema.

</wsdl:documentation>

<wsdl:part name=”MessageHeaderType”

element=”header:MessageHeaderType” />

</wsdl:message>

And to include that in the input message:

<wsdl:input>

<soap:header message=”tns:MessageHeaderType”

part=”MessageHeaderType” use=”literal” />

<soap:body use=”literal” />

</wsdl:input>

For Soap 1.1 the namespace have to be

xmlns:soap=http://schemas.xmlsoap.org/wsdl/soap/

NOTE: this can be used with SOAP1.2 as well but you just need to update the WSDL bindings etc.

I will try and post a SOAP1.2 WSDL as well. But that’s for another day.

I use the O2 Broadband service its dirt cheap and service is so very good I would recommend the it to one and all.
http://nitinaggarwal.wordpress.com

2 Responses so far.

  1. Lakshmi Arun says:

    hi
    can you also post the headerschema xsd. I want to add a custom header to my wsdl. Also do you have any idea on WS-addressing . How to implement it in existing wsdl.

  2. Looks like you are an expert in this field, you got some great points there, but you’ll want to add a facebook button to your blog. I just bookmarked this article, although I had to complete it manually. Simply my $.02 🙂

    – Daniel