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>

Now For adding a Custom soap header
Firstly we need to define the custom header type and import any refernces:


<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>

Then the last thing is to add the soap:header element in the <wsdl:input> if you only want the header for request.
Also add it to <wsdl:output> as well if you need it for both request and response.


<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.

Advertisements

2 comments

  1. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s