【问题标题】:Mule ESB - http:outbound-endpoint with post parametersMule ESB - 带有 post 参数的 http:outbound-endpoint
【发布时间】:2014-10-30 08:31:58
【问题描述】:

我是 Mule ESB 的新手,我正在尝试在 POST 中调用一个 PHP 脚本,该脚本需要一个名为 json 的参数。

我的 Mule Flow xml 文件看起来像这样,如何将参数添加到我的请求中?

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" version="EE-3.5.1" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" 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
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
    <flow doc:name="HelloWorldFlow1" name="HelloWorldFlow1">
        <http:inbound-endpoint doc:description="This endpoint receives an HTTP message." doc:name="HTTP" exchange-pattern="request-response" host="localhost" port="8081"/>
        <http:outbound-endpoint exchange-pattern="request-response" host="*someurl*" port="80" path="*somepath*" method="GET" doc:name="HTTP"/>
        <logger message="Logging #[message.payload]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

感谢您的帮助。

【问题讨论】:

  • 如果您选择的答案是您当时正在寻找的答案,那么它就是一个基本的 Mule HTTP 出站。在发布问题之前尝试进行研究。 StackOverflow Mule 标签已经多次解决了这种情况。

标签: json post parameters mule esb


【解决方案1】:

试试这个

<flow name="testpostFlow1" doc:name="testpostFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="testpost" doc:name="HTTP"/>
    <set-payload value="#[{&quot;json&quot;: {&quot;test&quot;: 12345, &quot;moreTest&quot;: &quot;sample data&quot;}}]" doc:name="Set Payload"/>
    <http:outbound-endpoint exchange-pattern="request-response" method="POST" address="http://www.htmlcodetutorial.com/cgi-bin/mycgi.pl" mimeType="application/x-www-form-urlencoded" doc:name="HTTP"/>
</flow>

或者,如果您已经将 JSON 作为流变量:

<flow name="testpostFlow1" doc:name="testpostFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="testpost" doc:name="HTTP"/>
    <set-variable variableName="json" value="#[{&quot;test&quot;: 12345, &quot;moreTest&quot;: &quot;sample data&quot;}]" doc:name="Variable"/>
    <set-payload value="#[{&quot;json&quot;: flowVars[&quot;json&quot;]}]" doc:name="Set Payload"/>
    <http:outbound-endpoint exchange-pattern="request-response" method="POST" address="http://www.htmlcodetutorial.com/cgi-bin/mycgi.pl" mimeType="application/x-www-form-urlencoded" doc:name="HTTP"/>
</flow>

【讨论】:

    【解决方案2】:

    您可以使用 HTTP outbouns 端点从 Mule 发布任何 JSON 请求。例如,如果您想发布以下 JSON 请求:-

    {
        "Data": 
            {
                "id": "6",
                "name": "ddddd",
                "age": "55",
                "designation": "WQQQQQ"
            }
    
    }
    

    现在你需要的是 Mule 中的以下内容:-

    <?xml version="1.0" encoding="UTF-8"?>
     <mule xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" version="EE-3.5.1" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" 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
    http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
     http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
    <flow doc:name="HelloWorldFlow1" name="HelloWorldFlow1">
    <http:inbound-endpoint doc:description="This endpoint receives an HTTP message." doc:name="HTTP" exchange-pattern="request-response" host="localhost" port="8081"/>
    <set-payload value=" {"Data":{"id": "6","name": "ddddd","age": "55","designation": "WQQQQQ"}}" doc:name="Set Payload"/> 
    <http:outbound-endpoint exchange-pattern="request-response" host="*someurl*" port="80" path="*somepath*" method="POST" contentType="application/json" doc:name="HTTP"/>
    <logger message="Logging #[message.payload]" level="INFO" doc:name="Logger"/>
    </flow>
     </mule>
    

    这会将 JSON 数据发布到外部端点。您需要在 http 出站端点中使用 contentType="application/json"method="POST"

    更新:-
    您需要在地址属性中的 http 出站端点的 url 中指定参数 .. 例如,如果 JSON PARAMETER 在 url 中,如:- http:8080//myservice?json=mydata .. 您需要在 http 地址属性中指定,如下所示 &lt;http:outbound-endpoint exchange-pattern="request-response" address="http:8080//myservice?json=mydata" method="POST" contentType="application/json" doc:name="HTTP"/&gt;

    【讨论】:

    • 它不起作用,我需要为 POST 请求的参数指定名称。 PHP 脚本需要一个名为 json 的参数的 JSON 字符串,我在哪里可以指定它的名称?
    【解决方案3】:

    添加&lt;set-property&gt; 标记以将属性添加到您的Mule 消息。这将与 HTTP 出站调用一起发送。

     <flow doc:name="HelloWorldFlow1" name="HelloWorldFlow1">
        <http:inbound-endpoint doc:description="This endpoint receives an HTTP message." doc:name="HTTP" exchange-pattern="request-response" host="localhost" port="8081"/>
        <set-property  name="json" value="your json string" />
        <http:outbound-endpoint exchange-pattern="request-response" host="*someurl*" port="80" path="*somepath*" method="GET" doc:name="HTTP"/>
        <logger message="Logging #[message.payload]" level="INFO" doc:name="Logger"/>
    </flow>
    

    希望这会有所帮助。

    【讨论】:

    • OP 想要使用 POST 方法,而不是 GET
    【解决方案4】:

    问题是关于http:outbound-endpoint,它已经过时了。它已被http:request 取代。您可以通过调整配置以适应您的环境,将您的 requiremnet 移植到新的组件中。

    <set-property propertyName="Content-Type" value="application/json" />
    <set-payload value="#[{&quot;json&quot;: flowVars[&quot;json&quot;]}]" doc:name="Set Payload"/>
    
    <http:request path="/api/v1/orders" method="POST" config-ref="requestConfig">
       <http:query-param paramName="json" value="1" />
       <!-- configure the elements you need -->
    </http:request>
    

    Mule 将使用查询发布参数json=1 将 json 内容发布到资源。

    https://api.company.com//api/v1/orders?json=1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 2014-04-09
      • 1970-01-01
      • 2012-09-18
      相关资源
      最近更新 更多