2

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

我的问题是:

1.我怎样才能让下面的例子工作?

2. 如何从 JSON 编码的有效负载创建带有 POST 参数的地图有效负载并将其发送到 PHP 脚本?

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

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

骡流

底层 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。

我已经审查了许多类似的问题,但没有一个让我走上正轨。

4

3 回答 3

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 脚本调用的所有响应标头传播回原始调用者。如果您不在乎,请删除它。

于 2013-02-18T17:37:19.060 回答
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"/>
于 2013-02-17T12:59:23.540 回答
0

问题有点老了,但只是遇到了同样的问题。我永远无法让“body-to-parameter-map-transformer”工作,所以我加入了一个自定义的 java 组件。它将 URLEncoded 参数字符串解析为 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;

    }

}
于 2015-01-07T23:10:21.273 回答