带有URL表单编码正文的Mule ESB HTTP POST请求

带有URL表单编码正文的Mule ESB HTTP POST请求,http,rest,mule,esb,http-post,Http,Rest,Mule,Esb,Http Post,我是MuleESB的新手,尝试向REST服务发送post请求。请求主体采用字符串形式,请求应为x-www-form-urlencoded。我将有效负载设置为我的参数,并将请求发送到REST服务,但出现异常: Failed to invoke REST service "http://username:password@192.168.10.252/api/rest/session/login.json". Message payload is of type: String 我的restURL

我是MuleESB的新手,尝试向REST服务发送post请求。请求主体采用字符串形式,请求应为x-www-form-urlencoded。我将有效负载设置为我的参数,并将请求发送到REST服务,但出现异常:

Failed to invoke REST service "http://username:password@192.168.10.252/api/rest/session/login.json". Message payload is of type: String
我的restURL是:(#[restURL])${dms.host}/api/Rest/session/login.json

我的参数是(设置为payload#[restBody]):username=user&password=pass

<sub-flow name="RESTrequestToDMS" doc:name="RESTrequestToDMS" processingStrategy="synchronous">
    <set-variable variableName="originalMessage" value="#[payload]" doc:name="Backup original message"/>
    <logger message="#[restBody]" level="INFO" doc:name="Logger"/>
    <choice doc:name="Choice">
        <when expression="restHTTPmethod == 'POST'">
            <processor-chain>    
                <set-payload value="#[restBody]" doc:name="Set Payload"/>           
                <http:rest-service-component httpMethod="POST" serviceUrl="http://${dms.user}:${dms.pass}@#[restURL]"></http:rest-service-component>
            </processor-chain>
        </when>
        <when expression="restHTTPmethod == 'GET'">
            <processor-chain>
                <http:rest-service-component httpMethod="GET" serviceUrl="http://${dms.user}:${dms.pass}@#[restURL]?#[restBody]"></http:rest-service-component>
            </processor-chain>
        </when>
        <otherwise>
            <processor-chain>
                <logger message="Unknown http method type is provided! " level="ERROR" doc:name="Logger"/>
            </processor-chain>
        </otherwise>
    </choice>
    <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
    <echo-component doc:name="Echo"/>
    <set-variable variableName="RESTResponse" value="#[payload]" doc:name="set RESTResponse"/>
    <set-payload value="#[flowVars['originalMessage']]" doc:name="Restore Original Message"/>
</sub-flow>
更新:通过添加一个值为“application/x-www-form-urlencoded”的消息属性“Content Type”,上述问题(例外)已经得到解决

下面的最后一部分对我来说仍然是个谜:)

另一件事是,正如我在对David的回答的评论中所说的,我可以以自适应的方式为rest组件添加所需的参数吗?更清楚地说,我不想为每个不同的请求创建不同的流。我在有效负载中有参数映射,所以我想对它们进行迭代,并将它们放入所需的参数中。这样的事情可能吗

提前多谢

--------------更新-----------------

所以,我修改了我的流程并使用了David的建议。然而,现在,我得到一个HTTP405错误(不允许使用方法)。但我肯定我是用正确的内容类型发布到了正确的地址。这是什么原因?有人有主意吗

进入这个流的有效负载是一个字符串(url编码的参数),例如->

sessionId=9eub9gm7k7oc1ub81dhef6t46q&xml=%3CObjectList%3E%3CFolderObject%3E%3CAddToFolder%20RefType%3D%22Path%22%20ClassName%3D%22FolderObject%22%3E%3C!%5BCATA%5B%2FolderPath%5D%5D%3E%3C%2AddToFolder%3E%3CName%3E%3C!%5BCATA%5Baaa%5D%5D%3E%3C%2FName%3E%3CDescription%3E%3C!%5BCATA%5B来自%20用户%20aaa%20的请求%20存储在%20此%20文件夹%5D%5D%3E%3C%2FDescription%3E%3Cnoerroriefist%2F%3E%3C%2FFolderObject%3E%3C%2FObjectList%3E中

<flow name="RESTrequestToDMS" doc:name="RESTrequestToDMS" processingStrategy="synchronous">
    <set-variable variableName="originalMessage" value="#[payload]" doc:name="Backup original message"/>
    <choice doc:name="Choice">
        <when expression="restHTTPmethod == 'POST'">
            <processor-chain>
                <set-payload value="#[restBody]" doc:name="Set Payload"/>
                <logger message="The payload before http post is: #[message.payload]" level="INFO" doc:name="Logger"/>
                <http:outbound-endpoint  exchange-pattern="request-response" doc:name="HTTP" address="http://#[restURL]"  user="${dms.user}" password="${dms.pass}" contentType="application/x-www-form-urlencoded">
                </http:outbound-endpoint>
            </processor-chain>
        </when>
        <when expression="restHTTPmethod == 'GET'">
            <processor-chain>
                <http:outbound-endpoint method="GET"  exchange-pattern="request-response" doc:name="HTTP" address="http://#[restURL]?#[restBody]"  user="${dms.user}" password="${dms.pass}" contentType="application/x-www-form-urlencoded">
        </http:outbound-endpoint>
            </processor-chain>
        </when>
        <otherwise>
            <processor-chain>
                <logger message="Unknown http method type is provided! " level="ERROR" doc:name="Logger"/>
            </processor-chain>
        </otherwise>
    </choice>
    <echo-component doc:name="Echo"/>
    <choice doc:name="Choice">
        <when expression="payload != null &amp;&amp; payload != empty &amp;&amp; payload != ''">
            <processor-chain>
                <json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.Map"/>
                <set-variable variableName="RESTResponse" value="#[payload]" doc:name="set RESTResponse"/>
            </processor-chain>
        </when>
        <otherwise>
            <processor-chain>
                <logger message="The payload is null or empty! HTTP response code is: #[message.inboundProperties['http.status']]" level="INFO" doc:name="Logger"/>
                <set-variable variableName="RESTResponse" value="#[message.inboundProperties['http.status']]" doc:name="Set HTTP Status Code"/>
            </processor-chain>
        </otherwise>
    </choice>
    <set-payload value="#[flowVars['originalMessage']]" doc:name="Restore Original Message"/>
</flow> 

以下是如何通过rest服务组件传递要进行URL编码的参数:

<http:rest-service-component httpMethod="POST"
    serviceUrl="http://${dms.user}:${dms.pass}@#[restURL]">
    <http:requiredParameter key="username" value="user" />
    <http:requiredParameter key="password" value="pass" />
</http:rest-service-component>


我不明白
http:object to http request transformer
/
http:outbound endpoint
对子流开头的
的用途。也不要使用
echo组件
来记录,使用
记录器
消息处理器。实际上我已经删除了那行,但忘了在这里更改。你有什么建议来解决这个问题吗?这一直困扰着我,我找不到一个解决方案来在我的请求中附加一个字符串。谢谢你更新你的问题。尽管如此,我还是不明白为什么子流中仍然有两个出站HTTP调用(一个是使用
HTTP:outbound endpoint
,另一个是使用
HTTP:rest服务组件
)。你真的想调用远程服务两次吗?不,出站组件是我的错。我实际上只是想打电话给REST服务,但我不知道该怎么做。调用GET请求服务相当简单,但我不知道如何使用字符串体处理POST请求。谢谢您的回答。有没有办法使其适应变量数量的变化?我试图将这部分实现为一个子流,并在URL中发送所有请求的参数。有没有一种有效的方法来解决这类问题?我已经有了URI参数的映射。那么现在我必须一个接一个地设置requiredparameter选项吗?我发现我不能在组件内部使用foreach,因此我不确定是否必须一次映射所有这些。那么最好是使用出站HTTP端点,并在MEL组件中自己构建URL。您能给我一个如何使用我拥有的参数映射构建URL的示例吗?我不太清楚该怎么做。
这就行了。谢谢:)
<flow name="RESTrequestToDMS" doc:name="RESTrequestToDMS" processingStrategy="synchronous">
    <set-variable variableName="originalMessage" value="#[payload]" doc:name="Backup original message"/>
    <choice doc:name="Choice">
        <when expression="restHTTPmethod == 'POST'">
            <processor-chain>
                <set-payload value="#[restBody]" doc:name="Set Payload"/>
                <logger message="The payload before http post is: #[message.payload]" level="INFO" doc:name="Logger"/>
                <http:outbound-endpoint  exchange-pattern="request-response" doc:name="HTTP" address="http://#[restURL]"  user="${dms.user}" password="${dms.pass}" contentType="application/x-www-form-urlencoded">
                </http:outbound-endpoint>
            </processor-chain>
        </when>
        <when expression="restHTTPmethod == 'GET'">
            <processor-chain>
                <http:outbound-endpoint method="GET"  exchange-pattern="request-response" doc:name="HTTP" address="http://#[restURL]?#[restBody]"  user="${dms.user}" password="${dms.pass}" contentType="application/x-www-form-urlencoded">
        </http:outbound-endpoint>
            </processor-chain>
        </when>
        <otherwise>
            <processor-chain>
                <logger message="Unknown http method type is provided! " level="ERROR" doc:name="Logger"/>
            </processor-chain>
        </otherwise>
    </choice>
    <echo-component doc:name="Echo"/>
    <choice doc:name="Choice">
        <when expression="payload != null &amp;&amp; payload != empty &amp;&amp; payload != ''">
            <processor-chain>
                <json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.Map"/>
                <set-variable variableName="RESTResponse" value="#[payload]" doc:name="set RESTResponse"/>
            </processor-chain>
        </when>
        <otherwise>
            <processor-chain>
                <logger message="The payload is null or empty! HTTP response code is: #[message.inboundProperties['http.status']]" level="INFO" doc:name="Logger"/>
                <set-variable variableName="RESTResponse" value="#[message.inboundProperties['http.status']]" doc:name="Set HTTP Status Code"/>
            </processor-chain>
        </otherwise>
    </choice>
    <set-payload value="#[flowVars['originalMessage']]" doc:name="Restore Original Message"/>
</flow> 
<http:rest-service-component httpMethod="POST"
    serviceUrl="http://${dms.user}:${dms.pass}@#[restURL]">
    <http:requiredParameter key="username" value="user" />
    <http:requiredParameter key="password" value="pass" />
</http:rest-service-component>