2014-09-05 86 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