2014-09-05 123 views
1

我對Mule ESB相當陌生,我試圖在POST中調用一個PHP腳本,該腳本需要一個名爲json的參數。Mule ESB -http:outbound-endpoint with post parameters

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> 

謝謝你的幫助。

+0

如果selcted答案是什麼你要找的話,這是一個基本騾子HTTP出站。在發佈問題之前嘗試研究。 StackOverflow Mule標籤已經多次解決這種場景。 – user1760178 2014-09-08 17:37:28

回答

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

可以使用HTTP outbouns端點。例如從發佈任何騾JSON請求,如果您想發表以下JSON請求: -

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

} 

現在你需要的是在騾如下: -

<?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 = 「應用/ JSON」方法= 「POST」在HTTP出站端點

更新: -
你需要一個像到指定地址屬性在HTTP出站端點的URL參數..例如,如果JSON參數在網址: - http:8080//myservice?json=mydata ..你需要在HTTP指定這個地址屬性如下<http:outbound-endpoint exchange-pattern="request-response" address="http:8080//myservice?json=mydata" method="POST" contentType="application/json" doc:name="HTTP"/>

+0

它不起作用,我需要指定POST請求參數的名稱。 PHP腳本需要一個名爲json的參數的JSON字符串,我可以在哪裏指定它的名稱? – iMahdi 2014-09-05 13:39:26

+0

請檢查更新後的答案 – 2014-09-05 16:05:25

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> 

希望這會有所幫助。

+0

OP要使用POST方法,而不是GET – 2014-09-06 07:57:41