Basic JSON to XML query

I am building a soap request in Target Flow Request .. assign message policy and setting a header in it as,

        <Headers>
            <Header name="Content-Type">application/xml</Header>
        </Headers>

In target flow response I am converting xml to json using xml2json policy. So in response I am getting JSON.

Now based on header content-type I also need to get xml as response. So on response flow I am using json2xml with following condition,

<Step>
                    <Condition> (request.header.Content-Type == "text/xml")</Condition>
                    <Name>JSON2XML</Name>
                </Step>

But when I give the above header i still get JSON, in trace I see that the JSON2XML policy skipped because it is still taking the content-type as application/json from Assign message policy.

Trace of skipped json2xml policy shows,

request.header.Content-Type       application/xml

Any idea what to do?

Solved Solved
1 6 1,148
1 ACCEPTED SOLUTION

nmunro
New Member

@Barahalikar Siddharth,

You state that the skipped json2xml policy shows the request header content type to be application/xml in the trace. In that case, should your Condition for executing the json2xml policy not be using "application/xml" rather than "text/xml"?

<Condition> (request.header.Content-Type = "application/xml")</Condition>

If not the above then I would redesign a little.

I would try adding an extract variables policy in the proxy request to capture the client's requested content type as a variable. (Add the extract variables policy in the proxy request flow)

<ExtractVariables name='ExtractVariables.RequestedContentType'>
  <Source>request</Source>
  <Header name="Accept">
    <Pattern ignoreCase="true">{contentType}</Pattern>
  </Header>
  <VariablePrefix>myproxyprefix</VariablePrefix>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables
</ExtractVariables>

I'm making an assumption here that your proxy expects and receives an Accept header.

If you are relying on the Content-Type header because there's no Accept header and the proxy request is a POST then amend the above from Accept to Content-Type.

You could then refer to this variable in the condition for the json2xml policy rather than the request.header (your code amended below, note that I use the single = as per @Anil Sagar's answer)

<Step>
                    <Condition> (myproxyprefix.contentType = "text/xml")</Condition>
                    <Name>JSON2XML</Name>
                </Step>

Although I don't know the detail, I think you may want to think about your conversions. It seems odd that you are converting SOAP (XML) to json and then back to XML again should the client request it.

Assuming you won't be returning the SOAP envelope to the client in either the JSON or the XML response, why not convert the SOAP target response into the XML format you require for the client and then send XML as the response by default?

Then, you would only need an xml2json conversion that would be for the case where the original request specified application/json.

This might simplify your flow conditions and remove some of the AssignMessage policies that are setting and resetting headers.

View solution in original post

6 REPLIES 6

@Barahalikar Siddharth ,

Try with "=" instead of "==" . For more details refer docs here.

@Anil Sagar, it did not work. I have also tried equals.

Not applicable

what is your accept type and content type whenyou are sending Request to you end point?

Can you Try this ?

request.header.Accept == "application/xml" or request.header.Accept == "text/xml"

Regards

Binaya

nmunro
New Member

@Barahalikar Siddharth,

You state that the skipped json2xml policy shows the request header content type to be application/xml in the trace. In that case, should your Condition for executing the json2xml policy not be using "application/xml" rather than "text/xml"?

<Condition> (request.header.Content-Type = "application/xml")</Condition>

If not the above then I would redesign a little.

I would try adding an extract variables policy in the proxy request to capture the client's requested content type as a variable. (Add the extract variables policy in the proxy request flow)

<ExtractVariables name='ExtractVariables.RequestedContentType'>
  <Source>request</Source>
  <Header name="Accept">
    <Pattern ignoreCase="true">{contentType}</Pattern>
  </Header>
  <VariablePrefix>myproxyprefix</VariablePrefix>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables
</ExtractVariables>

I'm making an assumption here that your proxy expects and receives an Accept header.

If you are relying on the Content-Type header because there's no Accept header and the proxy request is a POST then amend the above from Accept to Content-Type.

You could then refer to this variable in the condition for the json2xml policy rather than the request.header (your code amended below, note that I use the single = as per @Anil Sagar's answer)

<Step>
                    <Condition> (myproxyprefix.contentType = "text/xml")</Condition>
                    <Name>JSON2XML</Name>
                </Step>

Although I don't know the detail, I think you may want to think about your conversions. It seems odd that you are converting SOAP (XML) to json and then back to XML again should the client request it.

Assuming you won't be returning the SOAP envelope to the client in either the JSON or the XML response, why not convert the SOAP target response into the XML format you require for the client and then send XML as the response by default?

Then, you would only need an xml2json conversion that would be for the case where the original request specified application/json.

This might simplify your flow conditions and remove some of the AssignMessage policies that are setting and resetting headers.

Thank you @Neil Munro, it worked and yes I have only used xml2json.

Can you help me with, this question?

https://community.apigee.com/questions/27565/cache-key-query.html

Former Community Member
Not applicable

I wouldn't match to application/xml or text/xml. Many systems actually set the content type to "text/xml;charset=utf-8". Many combinations are possible. I think this is a good place to use RegEx. RegEx can be used in conditions by using ~ instead of =.