MashZone NextGen 10.2 | Appendix | Legacy Presto components | Mashables and Mashups | Mashups in EMML | Advanced Mashup Techniques | Handling JSON Responses or Inputs | Post JSON to a Web Service Using <directinvoke>
 
Post JSON to a Web Service Using <directinvoke>
For web services that require JSON input parameters, you can use <directinvoke> and the POST method with a variable that has subtype="JSON", such as this example:
...
<variable name="postBody" type="document" subtype="JSON">
<![CDATA[
{"locations": [
{"city":"San Francisco","lat":"37.7668","long":"-122.3959" },
{"city":"Albuquerque","lat":"35.151317","long":"-106.539867"},
{"city":"Seattle","lat":"47.61132","long":"-122.320637"}
]}
]]>
</variable>

<directinvoke endpoint="http://someOrg.com/jsonService" method="POST"
requestbody="$postBody" outputvariable="svcResponse"/>
...
In this example, the JSON is defined directly in the variable, but you can also use <assign> or <constructor> to assemble or build a JSON object.
You can also set the datatype for the response from the web service to JSON, simply by declaring the datatype and subtype of the variable to receive the response:
...
<variable name="postBody" type="document" subtype="JSON">
<![CDATA[
{"locations": [
{"city":"San Francisco","lat":"37.7668","long":"-122.3959" },
{"city":"Albuquerque","lat":"35.151317","long":"-106.539867"},
{"city":"Seattle","lat":"47.61132","long":"-122.320637"}
]}
]]>
</variable>
<variable name="svcResponse" type="document" subtype="JSON"/>

<directinvoke endpoint="http://someOrg.com/jsonService" method="POST"
requestbody="$postBody" outputvariable="svcResponse"/>
...

Copyright © 2013-2018 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.
Innovation Release