Mule ESB - How to create a HTTP request with POST

2019-02-19 22:31发布

问题:

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

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.

回答1:

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.



回答2:

Have you tried configuring your outbound endpoint like this:

<http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="80" path="post-debug.php" contentType="application/x-www-form-urlencoded" doc:name="HTTP" method="POST"/>


回答3:

Questions a little old, but just hit the same problem. I never could get the "body-to-parameter-map-transformer" to work, so I threw in a custom java component. It parses a URLEncoded param string into a HashMap. Set your vars based on that.

import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;


public class ParseParams {

    public static HashMap<String, String> jsonToMap(String t) throws JSONException {

        HashMap<String, String> map = new HashMap<String, String>();
        JSONObject jObject = new JSONObject(t);
        Iterator<?> keys = jObject.keys();

        while( keys.hasNext() ){
            String key = (String)keys.next();
            String value = jObject.getString(key); 
            map.put(key, value);

        }

        return map;

    }

}