Using DataPower XSLT extension to add custom Headers

N.B. this tutorial assumes the reader has some knowledge of DataPower.

IBM WebSphere DataPower SOA Appliances provides, apart from the XSLT1.0 extension functions, a wide range of DataPower specific XSLT extensions functions. Anyone who has got their hands dirty trying to do DataPower programming using XSLT knows that there are many ways to solve a problem.

In this post i will explain how to add a custom header to a request/response. More specifically i will add a custom response header to an HTTP request. If you have gone through the DataPower Extension Elements and Functions Catalog knows that there are 2 options available -

  1. set-http-request-header and set-http-response-header
  2. set-request-header and set-response-header

The later being for both HTTP and MQ.

I will be using the set-response-header in this example. This is particularly useful when dealing with web-services.

We will consider the following scenario –

  1. User makes a simple HTTP POST to a DataPower HTTP FSPH
  2. The backend of the DataPower config echoes the POST message
  3. On the response rule we add a custom header

This way we can verify the header getting added without having to enable the debug logs or probe on the appliance.

The policy looks like the one below.

addHeader

Under the Server to Client rule, which is our response rule, I have added a Transform action. The style sheet with in the Transform action just adds a custom header to the response rule. This is the XSLT that I used

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:dp="http://www.datapower.com/extensions"
                extension-element-prefixes="dp"
                exclude-result-prefixes="dp">        
<xsl:template match="/">
<xsl:variable name="s" select="dp:variable('var://service/routing-url')" />
<dp:set-http-response-header name="'SomeRandomlyAddedHeader'" value="$s"/>
</xsl:template>
</xsl:stylesheet>

We are adding an additional header “’SomeRandomlyAddedHeader”. When we place the POST request to the DataPower service we get the headers as shown below –

addHeaderRequest

  • SK

    Hi Jerry,

    I have a very similar scenario which I am working with. When I was searching, got your blog. When I try to get this working in my scenario, I am managed to get an error

    ‘Unrecognized XSLTC extension ‘http://www.datapower.com/extensions:set-http-response-header'

    I am using xsl version 2.0 and trying to customize our response. Thanks in advance.

    • Anonymous

      SK,
      DataPower doesn’t support XSLT v2.0. Can you switch to 1.0 and let me know if you’re getting the same error?

  • algol1983

    Hi Jerry,
    I have a similar task with little difference – I want to add http headers into MDN message. Can I do that using Datapower XSLT extension? Thanks.

    • Anonymous

      Since you mentioned MDN, I guess you’re using XB60 for some B2B Transaction.

      Well we can do that while requesting Asyc MDN since we can provide an MDN Redirection URL. Create a MPGW and redirect the MDN to it. While at the MPGW you can add your custom headers.

      Is your scenario different?

      • algol1983

        Yes, my scenario differ a little: I want to add http headers into outgoing MDN. Is it possible?

  • Deepsdiv S4

    how to create a reusable service to log messages