Mule ESB - http:带有post参数的outbound-endpoint

时间:2014-09-05 11:34:35

标签: json post parameters mule esb

我对Mule ESB很新,我试图在POST中调用一个需要一个名为json的参数的PHP脚本。

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

感谢您的帮助。

4 个答案:

答案 0 :(得分:2)

试试这个

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

答案 1 :(得分:1)

您可以使用HTTP outbouns端点从Mule发布任何JSON请求。例如,如果您要POST以下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数据发布到外部端点。您需要使用 contentType =&#34; application / json&#34; 方法=&#34; POST&# 34; 在http出站端点

更新: -
您需要在地址属性中的http出站端点的url中指定参数..例如,如果JSON PARAMETER在url中,则为: - http:8080//myservice?json=mydata ..您需要在http地址属性中指定此项,如下所示{{ 1}}

答案 2 :(得分:0)

添加<set-property>标记以向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>

希望这有帮助。

答案 3 :(得分:0)

问题是关于http:outbound-endpoint,它已经过时了。它已由http:request代替。您可以通过根据环境调整配置,将您的requiremnets移植到新组件。

<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