Saurabh Jhunjhunwala
Saurabh Jhunjhunwala

Reputation: 2922

How to POST to REST api with json payload from Mule Flow?

I am pretty new to Mule and need to stitch some micro-services to achieve a business objective. In this regard, my services are REST based and have a JSON payload. Below is my configuration ( used so far ) so as to invoke the service from mule.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="9001" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" host="10.208.165.126" port="8080" basePath="/ingestion" doc:name="HTTP Request Configuration"/>
    <flow name="ingestionsampleFlow" >
        <http:listener config-ref="HTTP_Listener_Configuration" path="/ingestion/" doc:name="HTTP"/>
        <set-payload doc:name="Set Payload" value="#[payload.user]"/>
       <!--  <json:json-to-object-transformer returnClass="java.util.Map" mimeType="application/json" doc:name="JSON to Object"/>

        <http:request config-ref="HTTP_Request_Configuration" path="esb" method="POST" doc:name="HTTP"/>
         -->

        <!--  <http:inbound-endpoint doc:name="HTTP" exchange-pattern="request-response" contentType="application/json" host="10.208.165.126" port="8080"/> -->

        <http:outbound-endpoint exchange-pattern="request-response" host="10.208.165.126" port="8080"  method="POST" contentType="application/json" doc:name="HTTP" path="esb"/>
        <logger message="2--&gt;#[payload]" level="INFO" doc:name="Logger"/>


    </flow>
</mule>

My service is hosted on 10.208.165.126:8080/ingestion/esb; but this does not even hit my service. This service has a payload which is of the below format : {"user":"saurabh"}

Please help me with the configuration and assist me in achieving my objective.

Upvotes: 1

Views: 6835

Answers (3)

Alex Fernandez
Alex Fernandez

Reputation: 1942

Use POSTMAN or any Rest client.

Upvotes: 1

John Anthony
John Anthony

Reputation: 56

I am not sure what is your data source but here is a simple example

enter image description here

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration_outboundWS" host="10.208.165.126" port="8080" doc:name="HTTP Request Configuration"/>
    <flow name="sampleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/inbound" doc:name="Inbound HTTP"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>doc:name="Set Payload"/>
        <set-property propertyName="Content-Type" value="application/json" doc:name="Set Outbound Header"/>
        <http:request config-ref="HTTP_Request_Configuration_outboundWS" path="/your/webservice/path" method="POST" doc:name="POST WebService"/>
    </flow>
</mule>

Upvotes: 2

John Anthony
John Anthony

Reputation: 56

  1. make your json data the current payload
  2. add properties before the HTTP Rest endpoint and set Content-Type application/json
  3. set HTTP outbound enpoint to POST
  4. fly away

Upvotes: 1

Related Questions