Mule ESB - How to create a HTTP request with POST method (sending parameters along)

Valentin Despa picture Valentin Despa · Feb 17, 2013 · Viewed 21.2k times · Source

Short: I want to post a couple of parameters (like user=admin, key=12345678) using the POST method to a PHP page (like localhost/post-debug.php). The script would read the $_POST values and do whatever.

My questions are:

1. How can I get the example below to work?

2. How can I create the Map Payload with POST parameters from a JSON encoded payload and send it to the PHP script?

Below is an isolated case I am trying to get running (the parameters are "read" from the HTTP endpoint). I am calling directly from the browser the following URL:

http://localhost:8081/httpPost?user=admin&key=12345678

MULE Flow

The underlying XML:

<?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" version="CE-3.3.1" 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 ">
    <flow name="httpPostTestFlow1" doc:name="httpPostTestFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="httpPost" doc:name="HTTP"/>
            <http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/>

        <echo-component doc:name="Echo"/>
        <http:outbound-endpoint exchange-pattern="request-response" host="localhost/post-debug.php" port="80"  contentType="application/x-www-form-urlencoded" doc:name="HTTP" />
    </flow>
</mule>

I am using MuleStudio 1.3.2, Mule ESB v.3.3.

I've reviewed many similar questions but none got me on the right track.

Answer

David Dossot picture David Dossot · Feb 18, 2013

Here is the solution for question 2 (answering question 1 won't help):

<flow name="httpPostTestFlow1">
    <http:inbound-endpoint exchange-pattern="request-response"
        host="localhost" port="8081" path="httpPost" />
    <json:json-to-object-transformer
        returnClass="java.util.Map" />
    <http:outbound-endpoint exchange-pattern="request-response"
        host="localhost" port="80" path="post-debug.php" method="POST"
        contentType="application/x-www-form-urlencoded" />
    <copy-properties propertyName="*" />
</flow>

I've used the following to check it works fine:

curl -H "Content-Type: application/json" -d '{"param1":"value1","param2":"value2"}' http://localhost:8081/httpPost

Note that I use copy-properties to propagate all the response headers from the PHP script invocation back to the original caller. Remove it if you don't care.