Search code examples
muleesb

how to get json from an application to my mule application


I want to link two applications with my mule application. so it must get information from the first one (msg, token), and add another information (email) and replay all this information to the second application. this is my code:

<db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="root" database="mulesoft" doc:name="MySQL Configuration"/>
    <smtp:gmail-connector name="Gmail" validateConnections="true" doc:name="Gmail"/>
<flow name="flows1Flow1">

    <http:inbound-endpoint host="localhost" port="8084"  encoding="UTF-8" doc:name="HTTP"/>
        <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
        <set-variable variableName="msg" value="#[message.payload.msg]" doc:name="Variable"/>
        <set-variable variableName="tkn" value="#[message.payload.tkn]" doc:name="Variable"/>
        <db:select config-ref="MySQL_Configuration" doc:name="Database">
            <db:parameterized-query><![CDATA[select * from push where token = #[message.payload['token']]]]></db:parameterized-query>
        </db:select>
        <foreach doc:name="For Each">
            <set-session-variable variableName="email" value="#[message.payload.email]" doc:name="Session Variable"/>
            <json:object-to-json-transformer doc:name="Object to JSON"/>
            <http:outbound-endpoint exchange-pattern="request-response" method="POST" address="http://slice.WS.com/api.php/push/send_test_push" contentType="application/json" doc:name="HTTP"/>
        </foreach>

</flow>

When i test with posting json information with postman (google chrome extension) i get this result: enter image description here

  1. What's wrong?
  2. How can i recover response from the second application (Web service) which is in json format?
    Thank you in advance.

Solution

  • You don't build a specific response object that can properly be serialized over HTTP so Mule uses what's produced by the latest message processor of the flow as the response.

    In your case, the for-each produces a List object and Mule dutifully serializes it as the HTTP response.

    You need to add an http:response-builder element and a set-payload message processor to create an HTTP response that makes sense to the caller of your inbound-endpoint.

    Example:

    <set-payload value="OK"/>
    <http:response-builder status="200" contentType="text/plain" />
    

    Reference: http://www.mulesoft.org/documentation/display/current/HTTP+Response+Builder