Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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]"/>
like image 797
Luca S. Avatar asked Nov 29 '22 22:11

Luca S.


2 Answers

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"/>

like image 78
Anirban Sen Chowdhary Avatar answered Dec 06 '22 06:12

Anirban Sen Chowdhary


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

Hope this helps.

like image 27
AnupamBhusari Avatar answered Dec 06 '22 05:12

AnupamBhusari