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

Mule ESB-如何使用POST方法创建HTTP请求(随附发送参数),mule,mule-studio,Mule,Mule Studio,简短:我想使用post方法将几个参数(比如user=admin,key=12345678)发布到PHP页面(比如localhost/post debug.PHP)。脚本将读取$\u POST值并执行任何操作 我的问题是: 1。我如何才能让下面的示例起作用? 2。如何从JSON编码的负载创建带有POST参数的映射负载,并将其发送到PHP脚本? 下面是我正在尝试运行的一个独立案例(参数是从HTTP端点“读取的”)。我直接从浏览器调用以下URL: http://localhost:8081/httpP

简短:我想使用post方法将几个参数(比如user=admin,key=12345678)发布到PHP页面(比如localhost/post debug.PHP)。脚本将读取$\u 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和MuleESBV.3.3


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

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

<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的解决方案(回答问题1没有帮助):


请注意,我使用
copy properties
将PHP脚本调用中的所有响应头传播回原始调用方。如果你不在乎的话,就把它删除。

问题有点老了,只是碰到了同样的问题。我从来都无法让“body-to-parameter-map-transformer”正常工作,所以我加入了一个自定义java组件。它将URL编码的参数字符串解析为HashMap。基于此设置您的VAR

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;

    }

}
import java.util.HashMap;
导入java.util.Iterator;
导入org.json.JSONException;
导入org.json.JSONObject;
公共类解析参数{
公共静态HashMap jsonToMap(字符串t)抛出JSONException{
HashMap=newHashMap();
JSONObject jObject=新的JSONObject(t);
迭代器keys=jObject.keys();
while(keys.hasNext()){
字符串键=(字符串)键。下一步();
字符串值=jObject.getString(键);
map.put(键、值);
}
返回图;
}
}

谢谢,但这篇文章运气不好。下面是我有时会遇到的错误:我刚刚注意到您设置的主机不是有效的主机。更新了相应的答案您可以使用HTTP代理模式解决问题1,但这对问题2不起作用。问题2是您希望运行的最终场景吗?如果是,回答问题1没有意义,我们可以只关注问题2。感谢您的反馈。是的,我想让场景2运行。我只是在给出的示例中尝试隔离问题。@DavidDossot当我尝试这段代码时,我得到了以下错误;无法从“json”转换为“java.util.Map”。消息负载的类型为:ContentLengthinInputStream(org.mule.api.transformer.TransformerMessageException)。消息负载的类型为:contentLengthinInputStream您是否将JSON对象发布到Mule?在http:outbound端点上运行良好。我一直在努力寻找能让它正常工作的东西。如果有可能让它工作,如果有人能提到如何解决它的链接或为什么它不是一个好主意的参考(可能REST不支持POST with x-www-form-urlencoded?)。嗨,有一种方法可以将此应用到HTTP连接器的最新版本吗?在这种情况下,用POST?
curl -H "Content-Type: application/json" -d '{"param1":"value1","param2":"value2"}' http://localhost:8081/httpPost
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;

    }

}