Why doesn't my XML to JSON policy work?

Not applicable

I'm trying to convert a XML payload to a JSON payload in my request flow, but even though in trace I can see that it goes through the policy, it doesn't convert the payload.

The call:

curl -X POST \
  https://{{url}} \
  -H 'apikey: j7VTDGijDuhyDiFrqy1qjSfvO9NJldHG' \
  -H 'authorization: Bearer CVfw1Ahm02OATzDdR9D3Agjk5eGR' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/xml' \
  -H 'postman-token: 0981c824-5900-99df-e403-5ba8c861ad60' \
  -d '<agreement_id>
9740
</agreement_id>'

My Policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLToJSON async="false" continueOnError="false" enabled="true" name="xml-to-json">
    <DisplayName>xml to json</DisplayName>
    <Properties/>
    <Format>yahoo</Format>
    <OutputVariable>request</OutputVariable>
    <Source>request</Source>
</XMLToJSON>

I send the following XML:

<agreement_id>
    {{agreement_id}}
</agreement_id>

And I expect it to be converted to:

{
    "agreementId": {{agreement_id}}
}
0 6 719
6 REPLIES 6

@Friso Denijs ,

Make sure Content-Type header is set to application/xml . If it's not sent in request, you can set using Assign Message policy.

Hope it helps. Keep us posted if any.

content type header was already set to application/xml

@Friso Denijs , Can you share the proxy bundle & sample request if possible ?

@Friso Denijs, change the "OutputVariable" to "response" instead of "request" in your XML2JSON policy.

@Friso Denijs, Did it work?

Here's a working API Proxy that shows XMLToJSON in action.

apiproxy-xmltojson-20181113-20210402-090137.zip

Invoke it llike this;

curl -i $endpoint/xmltojson-20181113/t1 -X POST -H content-type:application/xml -d '<agreement_id>9740</agreement_id>'

the result I see:

{"agreement_id":"9740"}