2016-12-29 217 views
0

我正在尝试使用Mule ESB发布multipart/form-data,但无法使其工作。我有相同的请求通过我的休息客户端和cURL成功地工作。Mule ESB multipart/form-data post not working

curl -i -X POST \ 
-H "Authorization:bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \ 
-H "X-ANYPNT-ORG-ID:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \ 
-H "X-ANYPNT-ENV-ID:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \ 
-H "Content-Type:multipart/form-data" \ 
-F "artifactName=test" \ 
-F "targetId=xxxxxx" \ 
-F "[email protected]\"./test-1.0.0-SNAPSHOT.zip\";type=application/x-zip-compressed;filename=\"test-1.0.0-SNAPSHOT.zip\"" \ 'https://anypoint.mulesoft.com/hybrid/api/v1/applications' 

我正在使用Transformer添加出站附件;

message.setPayload(NullPayload.getInstance()); 
message.addOutboundAttachment("artifactName", AttachmentHelper.getStringAttachment("artifactName", artifactName)); 
message.addOutboundAttachment("targetId", AttachmentHelper.getStringAttachment("targetId", targetId.toString())); 
message.addOutboundAttachment("file", AttachmentHelper.getURLAttachment("file", file)); 

public class AttachmentHelper { 
    public static DataHandler getStringAttachment(String name, String value) { 
    logger.info("Adding string attachment " + name + ": " + value); 
    byte[] bytes = value.getBytes(); 
    return new DataHandler(new ByteArrayDataSource(bytes, "text/plain", name)); 
    } 

    public static DataHandler getURLAttachment(String name, String value) throws Exception { 
    InputStream is = new URL(value).openStream(); 
    byte[] bytes = IOUtils.toByteArray(is); 
    return new DataHandler(new ByteArrayDataSource(bytes, "application/x-zip-compressed", name)); 
    } 
} 

然后只是拨打服务电话;

<http:request method="POST" path="/hybrid/api/v1/applications" config-ref="http_request_mule" doc:name="POST Deploy Application Call"> 
     <http:request-builder> 
      <http:header headerName="Authorization" value="bearer #[sessionVars.token]" /> 
      <http:header headerName="X-ANYPNT-ORG-ID" value="#[sessionVars.orgId]" /> 
      <http:header headerName="X-ANYPNT-ENV-ID" value="#[sessionVars.envId]" /> 
      <http:header headerName="Content-Type" value="multipart/form-data" /> 
     </http:request-builder> 
    </http:request> 

我得到一个HTTP状态400回来,我可以看到当我注释掉HTTP调用请求没有正确形成;

Unexpected character (-) at position 0. 
--null 
Content-Type: application/x-zip-compressed 

C:\DEV\zips\test-1.0.0-SNAPSHOT.zip (byte content removed for post) 
--null 
Content-Type: text/plain 

xxxxxx (value correct here just masked) 
--null 
Content-Type: text/plain 

test 
--null-- 

它似乎无法添加内容处置,附件名称和文件名。

我已经尝试过几乎所有事情,并且阅读所有内容,并且无所适从。有任何想法吗?

+0

不要明确添加内容类型。除非内容类型已经设置,否则多部分数据需要一个内部计算的边界元素。我怀疑是这个问题。尝试删除。 – afelisatti

+0

另外,这是哪个mule版本? – afelisatti

+0

不幸的是,没有工作,我仍然得到一个400.我在最新的3.8.2 EE。 –

回答

0

解决!我必须做的唯一改变就是添加文件名;

message.addOutboundAttachment("file", AttachmentHelper.getURLAttachment(FilenameUtils.getName(file), file));