darth_phoenixx
darth_phoenixx

Reputation: 952

Azure Api Management convert XML to JSON policy inbound

I am trying to define a policy in API to convert XML to JSON. Our back end API currently supports JSON only but I want clients to be able to post XML. I can successfully have JSON converted to XML as an outbound policy, but the inbound policy simply results in an empty post.

The working outbound policy is:

<outbound>
    <base />
    <json-to-xml apply="content-type-json" consider-accept-header="true" />
</outbound>

The inbound policy is effectively the reverse:

<inbound>
    <xml-to-json kind="direct" apply="content-type-xml" consider-accept-header="true" />
</inbound>

I have created a new test API pointing to a requestbin instance so I can inspect the request. I create a simply XML request in postman and set the Content-Type to text/xml, but the request in requestbin always shows an empty post.

I've tried debugging using the useful comment from @miao-jiang below. The trace shows that the policy is applied but that the content length has been removed. I wonder if this might be the root cause?

2Relevant entry from the trace log:

{"source":"xml-to-json", "timestamp":"2015-09-08T19:51:01.5919446Z", elapsed":"00:00:00.2116541", "data":"XML-to-JSON policy was applied. Original Content-Length header was removed as its value was invalidated. Content-Type header was set to 'application/json'."}

Here you can see that policy is applied, however no json is present in the request.

Upvotes: 4

Views: 5349

Answers (1)

Miao Jiang
Miao Jiang

Reputation: 633

I tried to reproduce your issue. Can you please confirm if your backend supports Transfer-Encoding: chunked?

What happens in the xml-to-json policy is that it removes your original content-length header, and replace it with Transfer-Encoding:chunked, because the length will be different after the transformation and we cannot predict the length.

When I tested with a backend that supports Transfer-Encoding:chunked, it worked properly. When I tested with another backend that does not support it, I saw the same behavior you described. So I was wondering if that’s what happened to you as well.

Upvotes: 5

Related Questions