Mule响应代码415被映射为具有文件的Rest post的失败

时间:2017-02-13 22:30:08

标签: mule

我有使用post操作的curl命令,如下所示。这在使用curl命令运行时工作正常。我将它转换为Mule代码,如下所示,但有一些问题。

curl -X POST -H "Authorization: bearer 61e888d88-9iu-2b1cd13a3cee" -H "X-ANYPNT-ORG-ID: 34edrf6-77f7-40fd-6c06a7bcf909" -H "X-ANYPNT-ENV-ID: cdf63246-69ed-4fc6-bbd8-" -H "Content-Type: 
multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -H "Cache-Control: no-cache" -H -F "file=@C:\myproject\zip\app-test.zip" 
-F "targetId=12345" -F "artifactName=myapp11" "https://anypoint.mulesoft.com/hybrid/api/v1/applications"

骡子代码:

<set-payload value="file=@C:\project\zip\app-test.zip&amp;targetId=12345&amp;artifactName=myapp11" doc:name="Set Payload"/>
<http:request config-ref="Hybrid" path="v1/applications" method="POST" doc:name="HTTP">
      <http:request-builder>
            <http:header headerName="Authorization" value="Bearer #[flowVars.accessToken]"/>
            <http:header headerName="X-ANYPNT-ORG-ID" value="#[flowVars.orgId]"/>
            <http:header headerName="X-ANYPNT-ENV-ID" value="#[flowVars.envId]"/>
            <http:header headerName="Cache-Control" value="no-cache"/>
      </http:request-builder>
</http:request>

错误消息:

ERROR 2017-02-13 12:54:45,605 [[deploy].HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Response code 415 mapped as failure.
Payload               : org.glassfish.grizzly.utils.BufferInputStream@1f9cdcd3
Element               : /deployFlow/processors/18 @ deploy:deploy.xml:61 (HTTP)
Element XML           : <http:request config-ref="Hybrid" path="v1/applications" method="POST" doc:name="HTTP">
                        <http:request-builder>
                        <http:header headerName="Authorization" value="Bearer #[flowVars.accessToken]"></http:header>
                        <http:header headerName="X-ANYPNT-ORG-ID" value="#[flowVars.orgId]"></http:header>
                        <http:header headerName="X-ANYPNT-ENV-ID" value="#[flowVars.envId]"></http:header>
                        <http:header headerName="Cache-Control" value="no-cache"></http:header>
                        </http:request-builder>
                        </http:request>
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.module.http.internal.request.ResponseValidatorException: Response code 415 mapped as failure.
    at org.mule.module.http.internal.request.SuccessStatusCodeValidator.validate(SuccessStatusCodeValidator.java:37)
    at org.mule.module.http.internal.request.DefaultHttpRequester.validateResponse(DefaultHttpRequester.java:356)
    at org.mule.module.http.internal.request.DefaultHttpRequester.innerProcess(DefaultHttpRequester.java:344)

更新

<http:request-config name="Hybrid" protocol="HTTPS" host="anypoint.mulesoft.com" port="443" basePath="hybrid/api/v1/"  doc:name="HTTPS req"/>

<message-properties-transformer overwrite="true" doc:name="Message Properties">            
    <add-message-property key="Content-Type" value="multipart/form-data;boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW;charset=UTF-8"/>
</message-properties-transformer>       
<set-attachment attachmentName="targetId" value="#[flowVars.targetId]" contentType="text/plain" doc:name="targetid"/>
<set-attachment attachmentName="artifactName" value="myapp4" contentType="text/plain" doc:name="artifactName"/>
<set-attachment attachmentName="file" value="@C:\myproject\zip\app-test.zip" contentType="application/zip" doc:name="file"/>
<http:request config-ref="Hybrid"   path="applications" method="POST" doc:name="HTTPS">
    <http:request-builder>
        <http:header headerName="Authorization" value="bearer #[flowVars.accessToken]"/>
        <http:header headerName="X-ANYPNT-ORG-ID" value="#[flowVars.orgId]"/>
        <http:header headerName="X-ANYPNT-ENV-ID" value="#[flowVars.envId]"/>
        <http:header headerName="Cache-Control" value="no-cache"/>
    </http:request-builder> 
</http:request>

错误消息:响应代码400映射为失败。

ERROR 2017-02-14 11:26:50,087 [[deploy].HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Response code 400 mapped as failure.
Payload               : org.glassfish.grizzly.utils.BufferInputStream@1eb91e03
Element               : /deployFlow/processors/21 @ deploy:deploy.xml:71 (HTTPS)
Element XML           : <http:request config-ref="Hybrid" path="applications" method="POST" doc:name="HTTPS">
                        <http:request-builder>
                        <http:header headerName="Authorization" value="bearer #[flowVars.accessToken]"></http:header>
                        <http:header headerName="X-ANYPNT-ORG-ID" value="#[flowVars.orgId]"></http:header>
                        <http:header headerName="X-ANYPNT-ENV-ID" value="#[flowVars.envId]"></http:header>
                        <http:header headerName="Cache-Control" value="no-cache"></http:header>
                        </http:request-builder>
                        </http:request>
--------------------------------------------------------------------------------

2 个答案:

答案 0 :(得分:0)

在curl命令中,您指定Content-Type: multipart/form-data,但这不太可能是Mule用于HTTP请求的原因,因为您只需将有效负载设置为字符串(不确定)它将默认为什么) - 这是HTTP 415 - 不支持的媒体类型建议的。

要使用multipart/form-data,您应该在启动HTTP请求之前而不是在有效负载中将表单元素(即targetIdartifactName等)添加到出站附件集合中。

请在此处查看我的答案:https://stackoverflow.com/a/42081258/5741477以获取示例。

答案 1 :(得分:0)

这是代码工作。你的curl命令是正确的。 这是我做的: curl -X POST -F“file=@testfile.txt”http://localhost:8081/special/file

如果您使用邮递员,则数据类型应为表格数据。

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="special-casesFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/special/file" allowedMethods="POST" doc:name="HTTP"/>
    <set-payload value="#[message.inboundAttachments]" doc:name="Retrieval Attachements"/>
    <foreach doc:name="For Each">

        <set-payload value="#[org.apache.commons.io.IOUtils.toByteArray(payload.getInputStream()); ]" doc:name="Convert File Stream To String"/>
        <byte-array-to-object-transformer />
        <object-to-string-transformer doc:name="Object to String"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    </foreach>
</flow>