I put an API proxy for http://mocktarget.apigee.net/json and enabled Json to xml policy. but I still see the response in json format

I am new to APIgee.

I put an API proxy for http://mocktarget.apigee.net/json and enabled Json to xml policy. but I still see the response in json format

10454-output-still-in-json.png

10453-jasontoxml-proxy.png

10452-apiproxysetting.png

1 13 715
13 REPLIES 13

@basam nath,

It seems you have not attached the Json-Xml policy in any of the response flows. see the text "Not used in any flows"

Pls check your trace session, it will reveal that the policy is not included.

Once you include it in either the Response Preflow or Response Postflow, you should be able to see the conversion.

Yes, and in the proxy editor, you can drag-and-drop to include the policy. You will want it in the Response flow, somewhere.

I did exactly what the sample video showed at docs.apagee.com

Once the JSON xml policy is created where can I add that policy to Response preflow and postflow?

thank you for replying Nagashree

I added the policy for "proxy endpoints" and "target endpoints" and it still doesn't show the response in the XML format.

10460-policy.png

10459-2020-10-12-16-11-36.png

mocktarget-rev1-2020-10-12.zipit looks like you have added XML-to-Json and Json-XML both the policies probably without a condition. so both policies are getting executed - first converting the json response to xml and then converting the xml to json - and the end result is Json which is what you are seeing in the response.

I have attached a sample here. you can try with that.

This is what I have in the sample, if response is JSON, convert to XML. if response is XML, convert to JSON

<Response>
            <Step>
                <Name>JSON-to-XML-1</Name>
                <Condition>(response.header.Content-Type Equals "application/json; charset=utf-8")</Condition>
            </Step>
            <Step>
                <Name>XML-to-JSON-1</Name>
                <Condition>(response.header.Content-Type Equals "application/xml; charset=utf-8")</Condition>
            </Step>
        </Response>

Here is the trace for the sample. Notice that only one of the policies XML-JSON or JSON-XML is executed based on the response content-type header. the other policy is skipped as the condition is not met.

mocktarget.jpg

so sorry for asking basic question.

Thank You for posting the zip file. How can I use this ? Is there a way I can import into Apigee?

Yes, when you create a new proxy in the edge UI - you will see the option to import from proxy bundle. you can upload this zip file in that option.

You can also create a bundle of your api proxy by using Project->Download Revision option. it will give you a zip file similar to the one I gave. This is usually referred to as a proxy bundle in Apigee terminology. hope this helps

I was able to import your bundle into Apigee. Your API response is hello guest. I have applied both xml to jason and jason to xml policiy just you did. mine she doesn't show the xml response.

I think I don't understand the preflow and post flow. I have to read up I think. it is very confusing.

You need to try with *eval-test.apigee.net/mocktarget/json and mocktarget/xml in the URL, then you will be able to see the xml and json responses. Another thing to note is, the XML to JSON policy will work only if the content-type is application/xml or text/xml. the JSON to XML policy will work only if the backend response content-type is application/json.

For hello guest, the content-type is text/plain; charset=utf-8. so both the policies will not execute.

NagaShree,

Thank You for sticking by and your kept responding basic questions from Newbie like me. Bless Your Heart. Truely appreciate for that.

After I have applied your changes to my API proxy, in Postman the expected XML response is working fine. But in browser it still shows up as JSON and not XML. What is the difference between Postman showing the response as XML vs Browser showing as JSON.

Also, this one completely is over my head

"thing to note is, the XML to JSON policy will work only if the content-type is application/xml or text/xml. the JSON to XML policy will work only if the backend response content-type is"

Where do I see "Content-type". on the host where the API is running physically? If so, this API is a public API running somewhere. so I won't be able to change that content type setting. Right?

Thank You and Kind regards for Your help.

basam



I dont think your api endpoint would behave differently whether you use postman or the browser. Would it be possible for you to share your api proxy bundle?

What I meant to state is that, the XML-JSON policy will not do anything if the backend response content-type were to be application/xml. For eg: in the "Hello guest" response scenario, the content-type from the backend is text/plain. XML-JSON policy even if executed has no change on the response payload.

You can view the headers sent from the backend and the payload in the trace. I have attached a screenshot

10466-mocktarget-backendresponse.jpg

As for the mocktarget backend, check this document which lists all the backend urls and also allows you to fetch sample responses. https://docs.apigee.com/api-platform/samples/mock-target-api.

For eg: if you try clicking on "Send Request" button on this url https://apidocs.apigee.com/mocktarget/apis/get/json, you will see the response in this format

HTTP/1.1 200 OK
Access-Control-Allow-Origin:*
Content-Length:13
Content-Type:text/plain; charset=utf-8
Date:Tue, 13 Oct 2020 00:09:51 GMT
ETag:W/"d-GHB1ZrJKk/wdVTdB/jgBsw"
Via:1.1 google
X-Content-Type-Options:nosniff
X-Frame-Options:ALLOW-FROM RESOURCE-URL
X-Powered-By:Apigee
X-XSS-Protection:1
{
  "firstName": "John",
  "lastName": "Doe",
  "city": "San Jose",
  "state": "CA"
}

mocktarget-rev3-2020-10-14.zip

This is the zipped file NagaShree.

basam