Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save steveosoule/b704c62c85963e2235c2a12d57ef0545 to your computer and use it in GitHub Desktop.

Select an option

Save steveosoule/b704c62c85963e2235c2a12d57ef0545 to your computer and use it in GitHub Desktop.
Miva - mvt:call POST json and parse returned json
<mvt:capture variable="l.request_json">
{
"foo": "bar",
"thing": 1
}
</mvt:capture>
<mvt:assign name="l.response" value="''" />
<mvt:call method="'RAW'" content-type="'application/json'" fields="'l.request_json'" action="'https://jsonplaceholder.typicode.com/posts/'">
<mvt:assign name="l.response" value="l.response $ s.callvalue" />
</mvt:call>
<h2>Raw Response</h2>
<pre>
<mvt:eval expr="encodeentities( l.response )" />
</pre>
<h2>Parsed Response</h2>
<mvt:assign name="l.responded_with_json" value="miva_json_decode( l.response, l.response_data )" />
<mvt:if expr="l.responded_with_json">
<ul>
<li>l.response_data:foo=<mvt:eval expr="encodeentities( l.response_data:foo )" /></li>
<li>l.response_data:thing=<mvt:eval expr="encodeentities( l.response_data:thing )" /></li>
<li>l.response_data:id=<mvt:eval expr="encodeentities( l.response_data:id )" /></li>
</ul>
<mvt:assign name="l.settings:_mvt_debug" value="glosub( miva_array_serialize( l.response_data ), ',', asciichar( 10 ) )" />
<pre>
@@debug l.response_data
&mvte:_mvt_debug;
</pre>
<mvt:else>
Error: Server did not respond with JSON
</mvt:if>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment