Search code examples
javajsonhttpmuleesb

Transforming Mule HttpRequest 'GET' Response


I'm making a httpRequest GET call in Mule to my own REST API and i would like to be able to transform the result into a JSONArray. The service I call is configured to return a JSON but when processing the payload in mule it is viewed as a BufferInputString.

Here is the code:

<http:request config-ref="HTTP_Request_Configuration" path="muletest" method="GET" metadata:id="ccb5da53-5418-9753-8399-bb6f8a792f0a" doc:name="HTTP">
        <http:request-builder>
            <http:query-param paramName="firstname" value="#[payload.firstname]"/>
            <http:query-param paramName="lastname" value="#[payload.lastname]"/>
        </http:request-builder>
</http:request>
<set-variable variableName="existingAppointment" value="#[payload]" encoding="UTF-8" mimeType="text/json" metadata:id="40cc3baf-834e-4a82-86db-2779fb736565" doc:name="Get Result"/>
<logger message="GET RESULT: #[payload.get(0)]" level="INFO" metadata:id="5b634d23-af82-4160-8351-4360aa3b3edf" doc:name="Log Get Result" />

Whenever I try to call #[payload.get(0)] to log the result jsonArray I get from the call, I have this exception:

ERROR 2016-02-23 16:23:00,673 [[omdcrv3].omdcrv3Flow.stage1.02] org.mule.exception.CatchMessagingExceptionStrategy: 

Message               : Execution of the expression "payload.get(0)" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: BufferInputStream
Type                  : org.mule.api.MessagingException
Code                  : MULE_ERROR--2
Payload               : org.glassfish.grizzly.utils.BufferInputStream@5d838090
JavaDoc               : http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html
********************************************************************************
Exception stack is:
1. [Error: unable to resolve method: org.glassfish.grizzly.utils.BufferInputStream.get(java.lang.Integer) [arglength=1]]
[Near : {... payload.get(0) ....}]

which obviously means that it's trying to process an object of type BufferInputStream but I don't understand how am I suppose to tranform this type to JSON type...

I'm sure I'm doing something wrong (i'm new to mule) but can't figure out what...

**** EDITED TO SHOW MY SOLUTION ****

Here is my SOLUTION


Solution

  • Hi you can use Dataweave (Transform Message) after the http request

    %dw 1.0
    %output application/json
    ---
    payload[0]
    

    Note you can replace your output to

    %output application/java
    

    The resulting payload will be an instance of LinkedHashMap