Mule Server 3.6 > Anypoint Studio > Raw JSON as POST data

647 views Asked by At

Still learning Mulesoft's Anypoint Studio... I am confused as how will I be able to access raw JSON POST data via the HTTP Listener then use the Choice flow control to execute conditions based on a value from a given JSON index. Anyone can show/tell me how to do this?

1

There are 1 answers

1
Ryan Carter On BEST ANSWER

The JSON HTTP body will automatically become the payload of your message in Mule probably represented as Stream.

Just for demo purposes, try logging the payload after your http:listener using:

<object-to-string-transformer />

<logger level="INFO" message="#[payload]" />

There best way to query JSON is to transform it to a Map suing the JSON module transformers.

<json:json-to-object-transformer returnClass="java.util.HashMap" />

And then query it using MEL like standard MVEL or Java syntax.

For a JSON document like: {"person" : {"name" : "bob"}}

<logger message="#[payload.person.name]" level="INFO" />

You can use these expressions in your choic router also:

<choice>
   <when expression="#[payload.person.name == 'bob']">
      do something ...
   </when>
</choice>