2017-02-13 60 views
0

我有捲曲命令與後操作如下所示。這在curl命令中運行時工作正常。我將它轉換爲Mule代碼,如下所示,但有一些問題。Mule響應代碼415映射爲失敗休息後與文件

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 "[email protected]:\myproject\zip\app-test.zip" 
-F "targetId=12345" -F "artifactName=myapp11" "https://anypoint.mulesoft.com/hybrid/api/v1/applications" 

騾代碼:

<set-payload value="[email protected]:\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    : [email protected] 
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) 

UPDATE:

<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    : [email protected] 
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> 
-------------------------------------------------------------------------------- 

回答

0

在你curl命令,你指定Content-Type: multipart/form-data,但是這不太可能是什麼騾子是使用HTTP請求,你只是簡單地設置有效載荷爲一個字符串(不知道它會默認實際上) - 這是HTTP 415 - 不受支持的媒體類型所暗示的。

要使用multipart/form-data您應該在啓動HTTP請求之前而不是在有效內容中將表單元素(即targetId,artifactName等)添加到出站附件集合中。

查看我的答案在這裏:https://stackoverflow.com/a/42081258/5741477爲例。

0

這是代碼工作。你的curl命令是正確的。 這裏是我做過什麼: 捲曲-X POST -F「[email protected]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> 
+0

關鍵是如果你想訪問附件,你必須使用:#[message.inboundAttachments] –