【问题标题】:WSO2 ESB HTTP POST with form data带有表单数据的 WSO2 ESB HTTP POST
【发布时间】:2015-08-11 19:33:09
【问题描述】:

我有一个肥皂服务,我想转身向外部服务器发布消息。

我可以像这样通过 curl 做到这一点:

curl  --data-urlencode "filename=data.txt" --data-urlencode "filedir=/home/myfile/in" 
      --data-urlencode "busproc=MyBP" --data-urlencode "serverip=192.168.1.4" 
      --data-urlencode"uid=myuserid" --data-urlencode "pwd=mypwd"
      http://somelocation.com:8833/webservice/inbound/here

但我不能让它正常工作。这是我的代理服务:

<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="ExampleHTTPPostWithFormData"
       transports="http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log/>
         <property name="messageType"
                   value="application/x-www-form-urlencoded"
                   scope="axis2"
                   type="STRING"/>
         <property name="HTTP_METHOD" value="post" scope="axis2" type="STRING"/>
         <send>
            <endpoint>
               <address uri="http://somelocation.com:8833/webservice/inbound/here"
                        format="pox"/>
               <property name="uid" value="user"/>
               <property name="pwd" value="password"/>
               <property name="filedir" value="/home/myfile/in"/>
               <property name="busproc" value="myBP"/>
               <property name="serverip" value="192.168.1.4"/>
               <property name="filename" value="data.txt"/>
            </endpoint>
         </send>
         <log level="full"/>
      </inSequence>
   </target>
   <description/>
</proxy>

最终服务似乎只看到我发布到 URL(而不是传入的数据属性)。

【问题讨论】:

    标签: wso2 esb synapse


    【解决方案1】:

    属性不是构建消息内容的方式。我发现最好的方法是使用payloadFactory。您需要构建的消息有一个根 XML 元素,每个表单字段有一个子元素,然后似乎 Axis2 通过以适当的格式序列化来处理application/x-www-form-urlencodedmessageType。因此,对您的代理的最小更改是:

    <proxy xmlns="http://ws.apache.org/ns/synapse"
           name="ExampleHTTPPostWithFormData"
           transports="http"
           statistics="disable"
           trace="disable"
           startOnLoad="true">
       <target>
          <inSequence>
             <log/>
             <property name="messageType"
                       value="application/x-www-form-urlencoded"
                       scope="axis2"
                       type="STRING"/>
             <payloadFactory media-type="xml">
               <format>
                 <params xmlns="">
                   <uid>user</uid>
                   <pwd>password</pwd>
                   <filedir>/home/myfile/in</filedir>
                   <busproc>myBP</busproc>
                   <serverip>192.168.1.4</serverip>
                   <filename>data.txt</filename>
                 </params>
               </format>
             </payloadFactory>
             <send>
                <endpoint>
                   <address uri="http://somelocation.com:8833/webservice/inbound/here"
                            format="rest"/>
                </endpoint>
             </send>
             <log level="full"/>
          </inSequence>
       </target>
       <description/>
    </proxy>
    

    根据您的 REST 服务是否处理 HTTP/1.1,添加 &lt;property name="FORCE_HTTP_1.0" value="true" scope="axis2" type="STRING"/&gt; 也可能很方便。

    如果您需要参数,则可以使用XPath extensions 将参数传递给payloadFactory。例如

             <payloadFactory media-type="xml">
               <format>
                 <params xmlns="">
                   <uid>user</uid>
                   <pwd>password</pwd>
                   <filedir>/home/myfile/in</filedir>
                   <busproc>myBP</busproc>
                   <serverip>192.168.1.4</serverip>
                   <filename>$1</filename>
                 </params>
               </format>
               <args>
                 <arg evaluator="xml" expression="$ctx:filename"/>
               </args>
             </payloadFactory>
    

    【讨论】:

      【解决方案2】:

      如果您在文件中发送 SOAP 有效负载,则需要使用 VFS 传输。请参考以下示例了解如何使用 VFS 传输来解决您的问题

      http://docs.wso2.org/pages/viewpage.action?pageId=26838852
      

      或者,您可以使用 SOAPUI 或任何 SOAP 客户端将有效负载直接发送到 ESB 代理端点

      【讨论】:

      • 为什么要使用 VFS 发布到网站?虽然上面有文件信息,但这不是目标。目标是使用表单数据进行 HTTP 发布。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多