Contact Free trial Login

To Set a SOAP Header

Use APIkit to set headers to your application’s response based on the WSDL declarations.

To follow this example, you must download the tshirt2.wsdl API definition, and create a Mule application based on it.
See Prerequisites for Using APIkit for Soap and Create an APIkit for SOAP Project for more information.

In this example, you add the APIUsageInformation header to the response of your application:

  1. In Studio, open the tshirt2.wsdl in src/main/resources/api and scroll to APIUsageInformation element, which is the element expected by the contract.

  2. On the canvas, select the Transform Message component in the OrderTshirt:/soapkit-config flow.

  3. In the existing Dataweave code:

    {
    	body : {
    		ns0#OrderTshirtResponse: {
    			orderId: "I got a request from "
    			++ payload.body.ns0#OrderTshirt.name
    			++ " using the following auth header "
    			++ (payload.headers["AuthenticationHeader"].ns0#AuthenticationHeader.apiKey default "")
    		}
    	} write "application/xml"
    }

    Add the APIUsageInformation header declaration:

    {
    	body : {
    		ns0#OrderTshirtResponse: {
    			orderId: "I got a request from "
    			++ payload.body.ns0#OrderTshirt.name
    			++ " using the following auth header "
    			++ (payload.headers["AuthenticationHeader"].ns0#AuthenticationHeader.apiKey default "")
    		}
    	} write "application/xml",
    
    	headers: { (1)
    		header: {
    			ns0#APIUsageInformation: {
    			}
    		} write "application/xml"
    	}
    
    }
    1 This piece of Dataweave code adds headers to the produced output in the Transform Message component.
  4. Add the apiCallsRemaining header to the APIUsageInformation element:

    {
    	body : {
    		ns0#OrderTshirtResponse: {
    			orderId: "I got a request from "
    			++ payload.body.ns0#OrderTshirt.name
    			++ " using the following auth header "
    			++ (payload.headers["AuthenticationHeader"].ns0#AuthenticationHeader.apiKey default "")
    		}
    	} write "application/xml",
    
    	headers: {
    		header: {
    			ns0#APIUsageInformation: {
    				apiCallsRemaining: 10 (1)
    			}
    		} write "application/xml"
    	}
    
    }
    1 This example sets apiCallsRemaining to 10.
  5. Save and rerun the project.

  6. In the SoapUI, execute the OrderTshirt request again. The response envelope from APIkit for SOAP is:

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <soap:Header>
          <ns0:APIUsageInformation xmlns:ns0="http://mulesoft.org/tshirt-service">
             <apiCallsRemaining>10</apiCallsRemaining>
          </ns0:APIUsageInformation>
       </soap:Header>
       <soap:Body>
          <ns0:OrderTshirtResponse xmlns:ns0="http://mulesoft.org/tshirt-service">
             <orderId>I got a request from John, using the following auth header 987654321</orderId>
          </ns0:OrderTshirtResponse>
       </soap:Body>
    </soap:Envelope>

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub