Hi,
I want to check if the JSON or XML are well formed and raise an error if they are not. Please suggest the ways I can do this in Edge.
Solved! Go to Solution.
If JSON, you can run a JS policy that simply performs JSON.parse().
If it works, then it is well formed.
Checkout SOAPMessageValidation policy - https://docs.apigee.com/api-platform/reference/policies/message-validation-policy#3:-well-formed-xml...; name of the policy may be confusing but you can use this policy to confirm that a JSON or XML message payload is well-formed (not the same as validation).
@Kuldeep Bhati We would need WSDL or XSD to validate XML using this policy? For JSON, I used JSON.parse() as suggsted by Dino.
mmm, yes.. But assessing the validity of an XML document is not what you asked for, is it? You asked about well-formed-ness. Validity is something beyond well-formed-ness.
I would think you could also use the JS policy to assess well-formed ness by attempting to use the asXML property. see here.
var x = context.proxyRequest.content.asXML; if (x == null) { throw new Error("not an XML document"); }
If JSON, you can run a JS policy that simply performs JSON.parse().
If it works, then it is well formed.
Yes, it worked. Thank you
Hey @Dino-at-Google -- Any reason you don't suggest the well-formedness feature of the SOAPMessageValidation policy? https://docs.apigee.com/api-platform/reference/policies/message-validation-policy#3:-well-formed-xml...
Hi @Shivakumar Sudi, yes I understand that, however XSD/WSDL is required for XML/SOAP message, not for JSON.
JSON Validation - Use the snippet mentioned below / also on documentation here - https://docs.apigee.com/api-platform/reference/policies/message-validation-policy#3:-well-formed-xml...
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <MessageValidation async="false" continueOnError="false" enabled="true" name="validateXMLorJSONRequest"> <DisplayName>validateXMLorJSONRequest</DisplayName> <Properties/> <Source>request</Source> </MessageValidation><br>
XML Validation - Use the snippet mentioned below / also on documentation here - https://docs.apigee.com/api-platform/reference/policies/message-validation-policy#1:-xsd-validation
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <MessageValidation async="false" continueOnError="false" enabled="true" name="validateXMLRequest"> <DisplayName>validateXMLRequest</DisplayName> <Properties/> <Source>request</Source> <ResourceURL>xsd://note-schema.xsd</ResourceURL> </MessageValidation>
I have just tested the both XML and well-formed JSON, check the attached sample proxy I just tried - xmljsonmessagevalidationproxy-v1-rev5-2019-03-22.zip deploy this proxy and test with curl command mentioned below for both JOSN/XML;
Use this curl call to test this proxy for JSON validation - where {proxy-endpoint} is your apigee proxy endpoint, e.g. http://{org}-{env}.apigee.net
curl -X POST \ {proxy-endpoint}/v1/validate-json-xml-message/echo \ -H 'Content-Type: application/json' \ -d '{ "to": "Fred Rogers", "from": "Nick Danger", "header": "Greetings from my neighborhood", "body": "Just writing to say hello." }'<br>
Use this curl call to test this proxy for XML validation - where {proxy-endpoint} is your apigee proxy endpoint, like
curl -X POST \ {proxy-endpoint}/v1/validate-json-xml-message/echo \ -H 'Content-Type: application/xml' \ -d '<note> <to>Fred Rogers</to> <from>Nick Danger</from> <heading>Greetings from my neighborhood</heading> <body>Just writing to say hello.</body> </note>'
I recommend using "SOAP Message Validation" and other OOTB policies over Javascript anyday, here's the explanation -
https://docs.apigee.com/api-platform/samples/cookbook/programming-api-proxies-javascript
I hope this explanation help.
Good luck!
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |