Luca S.
Luca S.

Reputation: 1162

How to convert org.glassfish.grizzly.utils.BufferInputStream to JSON in Mule?

On my first steps with Mule I'm writing a basic Http Proxy. Currently I forward the request to the api server and what I'd like to do is reading the payload that I receive from it before responding to the client.

When I try to log it with #[payload] it prints

 org.glassfish.grizzly.utils.BufferInputStream@2306df30

How can I print it properly in JSON format?

The full code:

<flow name="proxy">
    <http:listener config-ref="http-lc-0.0.0.0-8081" path="![p['proxy.path']]" parseRequest="false"/>

    <http:request config-ref="http-request-config" method="#[message.inboundProperties['http.method']]"
                  path="#[message.inboundProperties['http.request.path'].substring(message.inboundProperties['http.listener.path'].length()-2)]" parseResponse="false">
        <http:request-builder>
            <http:query-params expression="#[message.inboundProperties.'http.query.params']"/>
        </http:request-builder>
        <http:success-status-code-validator values="0..599" />
    </http:request>

    <logger doc:name="Logger" level="INFO" message="Payload #[payload]"/>

Upvotes: 4

Views: 18324

Answers (5)

Ayush Kumar
Ayush Kumar

Reputation: 964

One way to make sure that you get proper JSON response is to set payload as application/json mimetype.

<set-payload value="#[payload]" mimeType="application/json" doc:name="Set Payload"/>

Add this line after the HTTP Request. Just setting the payload value as #[payload] will convert it to json While converting it to string will just print it as string and not make it Json type.

Upvotes: 0

Swati Vatyani
Swati Vatyani

Reputation: 47

Simplest way is to just use <object-to-string-transformer doc:name="Object to String"/> after http request component and then place a logger with #[payload].

Upvotes: 0

Satheesh Kumar
Satheesh Kumar

Reputation: 797

The http component will send as InputStream,So use byte array to string transformer after http component.If you just want to print you can use #[message.payloadAs(java.lang.String)] but you wanna do any operation just drag and drop a byte array to string transformer

Upvotes: 0

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

The payload after HTTP request is generally in stream format, ref:- https://docs.mulesoft.com/mule-user-guide/v/3.7/http-request-connector
There are two ways you can get the payload after http:request

1) <object-to-string-transformer doc:name="Object to String"/> after http:request
or
2) using a logger and use MEL expression <logger message="#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>

Upvotes: 20

AnupamBhusari
AnupamBhusari

Reputation: 2415

Try #[message.payloadAs(java.lang.String)] which will log the expected output.

Hope this helps.

Upvotes: 3

Related Questions