Mule ESB - 如何使用POST方法创建HTTP请求(发送参数)

时间:2013-02-17 10:38:18

标签: mule mule-studio

简短:我想使用POST方法将一些参数(例如user = admin,key = 12345678)发布到PHP页面(如localhost / post-debug.php)。该脚本将读取$ _POST值并执行任何操作。

我的问题是:

1。如何让下面的例子起作用?

2。如何使用来自JSON编码的有效负载的POST参数创建Map Payload并将其发送到PHP脚本?

下面是一个我试图运行的孤立案例(参数是"读取"来自HTTP端点)。我直接从浏览器调用以下URL:

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

MULE Flow

基础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>

我使用的是MuleStudio 1.3.2,Mule ESB v.3.3。

我已经回顾了很多类似的问题,但没有一个让我走上正轨。

3 个答案:

答案 0 :(得分:7)

以下是问题2的解决方案(回答问题1无济于事):

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

我使用以下内容检查它是否正常工作:

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

请注意,我使用copy-properties将所有响应标头从PHP脚本调用传播回原始调用方。如果你不在乎,请将它取下。

答案 1 :(得分:0)

您是否尝试过这样配置出站端点:

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

答案 2 :(得分:0)

问题有点老,但只是遇到了同样的问题。我永远无法使“body-to-parameter-map-transformer”工作,所以我投入了一个自定义的java组件。它将URLEncoded param字符串解析为HashMap。根据它设置你的变种。

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;

    }

}
相关问题