【问题标题】:Mule ESB - How to create a HTTP request with POST method (sending parameters along)Mule ESB - 如何使用 POST 方法创建 HTTP 请求(一起发送参数)
【发布时间】:2013-02-01 22:24:03
【问题描述】:

简短:我想使用 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。

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

【问题讨论】:

  • 您可以使用 HTTP 代理模式解决问题 1,但这不适用于问题 2。问题 2 是您想要运行的最终方案吗?如果是,那么回答问题 1 没有意义,我们可以只关注问题 2。
  • 感谢您的反馈。是的,我想让场景 2 运行。我只是试图在给出的示例中找出问题。

标签: mule mule-studio


【解决方案1】:

这是问题 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 脚本调用传播回原始调用者。如果您不关心,请删除它。

【讨论】:

  • @DavidDossot 当我尝试此代码时,出现以下错误;无法从“json”转换为“java.util.Map”。消息负载的类型为:ContentLengthInputStream (org.mule.api.transformer.TransformerMessagingException)。消息负载的类型:ContentLengthInputStream
  • 您是否正在向 Mule 发布 JSON 对象
  • 与 http:outbound-endpoint 配合得很好。我一直在努力寻找一些东西来让它与 一起工作。如果有可能让它工作,那么如果有人可以提及如何解决它的链接或参考为什么它不是一个好主意(可能 REST 不支持带有 x-www-form-urlencoded 的 POST?),那就太好了。
  • 嗨,有没有办法将它应用到 HTTP 连接器的最新版本?在这种情况下使用 POST?
【解决方案2】:

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

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

【讨论】:

  • 谢谢,但 POST 不走运。这是我有时会遇到的错误:pastebin.com/LfJu8Kbt
  • 我刚刚注意到您设置的主机无效。相应地更新了答案
【解决方案3】:

问题有点老了,但遇到了同样的问题。我永远无法让 "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;

    }

}

【讨论】:

    猜你喜欢
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 2014-08-04
    • 2023-04-01
    相关资源
    最近更新 更多