0

I'm using ibm datapower to send a post request to an api. I'm using the url-open tag to send the post but I'm having trouble with the json payload. Ideally I'd like to do something like this:

<xsl:stylesheet xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" version="1.0">
  <dp:url-open target="{$url}">
    <json:object>
      <json:string name="key">value</json:string>
    </json:object>
  </dp:url-open>
</xsl:stylesheet>

but if I do it like this I'm getting an illegal char '{' error as the json isn't being stringified. If I do:

<xsl:variable name="payload">{"key": "value"}</xsl:variable>
<dp:url-open target="{$url}">
    <xsl:value-of select="$payload" />
</dp:url-open>

It works as expected but it's not very dynamic as I have to hardcode the stringified object. Is there a way to create the json object as per the first example and then stringify before sending the request?

Any ideas greatly appreciated

C

Cathal
  • 1,740
  • 18
  • 31

2 Answers2

0

DataPower comes with a stylesheet called store:///jsonx2json.xsl that you can use to convert. Like this:

<xsl:variable name="jsonx">
    <json:object>
        <json:string name="key">value</json:string>
    </json:object>
</xsl:variable>

<xsl:variable name="json">
    <xsl:copy-of select="dp:transform('store:///jsonx2json.xsl', $jsonx)"/>
</xsl:variable>

<dp:url-open target="{$url}">
    <xsl:value-of select="$json" />
</dp:url-open>
bjimba
  • 928
  • 8
  • 13
0

It is a bad practice to use dp:transform() for 'store:///jsonx2json.xsl'. Output of a xform action with that stylesheet should directly go to OUTPUT context. Reason is the differences of JSON and XML escaping. With dp:transform() you will most likely run into such escaping issues because the result JSON will be handled as an XML string.

Hermann.

HermannSW
  • 161
  • 1
  • 8