jon
jon

Reputation: 980

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

Upvotes: 0

Views: 2007

Answers (4)

Varun Narula
Varun Narula

Reputation: 57

Use an 'Object to JSON' transformer. That should work.

Upvotes: -1

sulthony h
sulthony h

Reputation: 1277

Even though the object type is BufferInputStream, if you put #[payload] in the logger (or Evaluate Mule Expression, Ctrl + Shift + I, when debug) you will get the JSON in form of String.

For further assessment, put #[json:] in the logger, you will also get the JSON. e.g.:

{
  "flights": [
    {
      "code": "ER38sd",
      "origin": "MUA",
      "destination": "SFO"
    },
    {
      "code": "ER39rk",
      "origin": "MUA",
      "destination": "SFO"
    }
  ]
}

Then modify the expression become #[json:flights], you will get an ArrayList (array of JSON in form of String)

Upvotes: 3

Alex Fernandez
Alex Fernandez

Reputation: 1942

You must convert the HttpResponse from JSON to Map so you could access it in mule expression:

 <json:json-to-object-transformer returnClass="java.util.HashMap" /> 
 <logger message="GET RESULT: #[payload.get(0)]" level="INFO" doc:name="Log Get Result" />

Upvotes: 1

vinzon
vinzon

Reputation: 148

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

Upvotes: 0

Related Questions