【问题标题】:Mule Response code 415 mapped as failure for Rest post with fileMule 响应代码 415 映射为带有文件的 Rest 发布失败
【发布时间】:2017-02-13 22:30:08
【问题描述】:

我有 curl 命令和 post 操作,如下所示。这在 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>
--------------------------------------------------------------------------------

【问题讨论】:

    标签: mule


    【解决方案1】:

    在您的 curl 命令中,您指定了 Content-Type: multipart/form-data,但这不太可能是 Mule 用于 HTTP 请求的内容,因为您只是将有效负载设置为字符串(不确定它实际上会默认为什么) - 这是 HTTP 415 - unsupported media type 所暗示的。

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

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

    【讨论】:

      【解决方案2】:

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

      如果你使用postman,数据类型应该是form-data。

      <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>
      

      【讨论】:

      • 关键是如果要访问附件,必须使用:#[message.inboundAttachments]
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多