iMahdi
iMahdi

Reputation: 338

Mule ESB - http:outbound-endpoint with post parameters

I'm fairly new to Mule ESB and i am trying to make a call, in POST, to a PHP script that requires a parameter called json.

My Mule Flow xml file looks like this, how do I add the parameter to my request ?

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" version="EE-3.5.1" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
    <flow doc:name="HelloWorldFlow1" name="HelloWorldFlow1">
        <http:inbound-endpoint doc:description="This endpoint receives an HTTP message." doc:name="HTTP" exchange-pattern="request-response" host="localhost" port="8081"/>
        <http:outbound-endpoint exchange-pattern="request-response" host="*someurl*" port="80" path="*somepath*" method="GET" doc:name="HTTP"/>
        <logger message="Logging #[message.payload]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

Thank you for your help.

Upvotes: 1

Views: 5552

Answers (4)

Ben Asmussen
Ben Asmussen

Reputation: 994

The question is about the http:outbound-endpoint, which is outdated. It has been replaced by the http:request. You can port your requiremnets to the new componet by adapting the configuration to your environment.

<set-property propertyName="Content-Type" value="application/json" />
<set-payload value="#[{&quot;json&quot;: flowVars[&quot;json&quot;]}]" doc:name="Set Payload"/>

<http:request path="/api/v1/orders" method="POST" config-ref="requestConfig">
   <http:query-param paramName="json" value="1" />
   <!-- configure the elements you need -->
</http:request>

Mule will post the json content to the resource with query post paramter json=1.

https://api.company.com//api/v1/orders?json=1

Upvotes: 0

Zlatin Zlatev
Zlatin Zlatev

Reputation: 3089

Try this

<flow name="testpostFlow1" doc:name="testpostFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="testpost" doc:name="HTTP"/>
    <set-payload value="#[{&quot;json&quot;: {&quot;test&quot;: 12345, &quot;moreTest&quot;: &quot;sample data&quot;}}]" doc:name="Set Payload"/>
    <http:outbound-endpoint exchange-pattern="request-response" method="POST" address="http://www.htmlcodetutorial.com/cgi-bin/mycgi.pl" mimeType="application/x-www-form-urlencoded" doc:name="HTTP"/>
</flow>

Or if you already have your JSON as a flow variable:

<flow name="testpostFlow1" doc:name="testpostFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="testpost" doc:name="HTTP"/>
    <set-variable variableName="json" value="#[{&quot;test&quot;: 12345, &quot;moreTest&quot;: &quot;sample data&quot;}]" doc:name="Variable"/>
    <set-payload value="#[{&quot;json&quot;: flowVars[&quot;json&quot;]}]" doc:name="Set Payload"/>
    <http:outbound-endpoint exchange-pattern="request-response" method="POST" address="http://www.htmlcodetutorial.com/cgi-bin/mycgi.pl" mimeType="application/x-www-form-urlencoded" doc:name="HTTP"/>
</flow>

Upvotes: 2

user1760178
user1760178

Reputation: 6647

Add the <set-property> tag to add a property to your Mule Message. This will be sent with the HTTP outbound call.

 <flow doc:name="HelloWorldFlow1" name="HelloWorldFlow1">
    <http:inbound-endpoint doc:description="This endpoint receives an HTTP message." doc:name="HTTP" exchange-pattern="request-response" host="localhost" port="8081"/>
    <set-property  name="json" value="your json string" />
    <http:outbound-endpoint exchange-pattern="request-response" host="*someurl*" port="80" path="*somepath*" method="GET" doc:name="HTTP"/>
    <logger message="Logging #[message.payload]" level="INFO" doc:name="Logger"/>
</flow>

Hope this helps.

Upvotes: 0

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

You can post any JSON request from Mule using HTTP outbouns endpoint .. For example if you want to POST the following JSON request :-

{
    "Data": 
        {
            "id": "6",
            "name": "ddddd",
            "age": "55",
            "designation": "WQQQQQ"
        }

}

Now what you need is the following in Mule :-

<?xml version="1.0" encoding="UTF-8"?>
 <mule xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" version="EE-3.5.1" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
 http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
<flow doc:name="HelloWorldFlow1" name="HelloWorldFlow1">
<http:inbound-endpoint doc:description="This endpoint receives an HTTP message." doc:name="HTTP" exchange-pattern="request-response" host="localhost" port="8081"/>
<set-payload value=" {"Data":{"id": "6","name": "ddddd","age": "55","designation": "WQQQQQ"}}" doc:name="Set Payload"/> 
<http:outbound-endpoint exchange-pattern="request-response" host="*someurl*" port="80" path="*somepath*" method="POST" contentType="application/json" doc:name="HTTP"/>
<logger message="Logging #[message.payload]" level="INFO" doc:name="Logger"/>
</flow>
 </mule>

This will post the JSON data to the external endpoint.. You need to use contentType="application/json" and method="POST" in http outbound endpoint

UPDATE :-
You need to specify the parameter in the url of http outbound endpoint in address attribute .. for example if JSON PARAMETER is in url like :- http:8080//myservice?json=mydata .. you need to specify this in http address attribute like the following <http:outbound-endpoint exchange-pattern="request-response" address="http:8080//myservice?json=mydata" method="POST" contentType="application/json" doc:name="HTTP"/>

Upvotes: 1

Related Questions