Mule http:outbound-endpoint + multipart/form-data

给你一囗甜甜゛ 提交于 2019-12-06 12:51:15

问题


I'm trying to call a rest service that expects to receive a multipart/form-data and I am facing some issues with this.

After a while searching, I've understood that I had to move the payload to an outbound attachment and set the payload to null.

  <expression-component><![CDATA[
        ds = new org.mule.message.ds.StringDataSource(formdata,'payload','multipart/form-data');
        dh = new javax.activation.DataHandler(ds);
        message.outboundAttachments['payload'] = dh;
    ]]></expression-component>          

    <set-payload value="#[null]" />

I've also done that, but got the error:

I received a content-type error: Message : Message contained MIME type "text/xml" when "multipart/form-data" was expected.

Obs.: I had to to use expression-component, because if I just use set-attachment I receive a " name must not be null" error.

Since the issue here was the type of the content, I also tried transforming the message properties.

    <message-properties-transformer overwrite="true" doc:name="Message Properties">
        <add-message-property key="Content-Type" value="multipart/form-data;charset=utf-8"/>
    </message-properties-transformer>           

After that, I received a 400 error from the rest service.

I've also tried to put each atribute (key-value) from the multipart in separated attachments as content type: text/plain, same issue here.

I guess my problem is how to attach correctly, so that the content type is transformed to multipart/form-data with success.

I appreciate any help. Thanks.

http outbound call code:

    <enricher source="#[message.inboundProperties['http.status']]"  target="#[variable:out]" doc:name="Message Enricher">
        <http:outbound-endpoint exchange-pattern="request-response" method="POST"  host="${jbpm.host}" port="${jbpm.port}" path="#[address]" doc:name="HTTP" mimeType="multipart/form-data">
            <response>
              <object-to-string-transformer />
            </response>
        </http:outbound-endpoint>
    </enricher>
    <logger message="RESPONSE STATUS - #[variable:out]" level="INFO" doc:name="Logger"/>
    <set-payload value="needAXml" doc:name="Set Payload"/>
    <mulexml:object-to-xml-transformer doc:name="Object to XML"/>

    <mulexml:xslt-transformer maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT">
        <mulexml:xslt-text>
            <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                                          xmlns:bpmsws="http://www.arizona.com.br/esb/bpmsws/service/v1_0_0/"
                                                          xmlns:bpms="http://www.arizona.com.br/bpms/core/v1_0_0/">
                <xsl:param name="responseStatus"/>

                <xsl:template match="/">
                    <bpmsws:httpPostResponse>
                        <status xmlns=""><xsl:value-of select="$responseStatus" /></status>
                    </bpmsws:httpPostResponse>
                </xsl:template>
            </xsl:stylesheet>
        </mulexml:xslt-text>
        <mulexml:context-property key="responseStatus" value="#[variable:out]"/>

    </mulexml:xslt-transformer>

回答1:


The following shows how to POST a multipart entity with two field values:

<expression-component><![CDATA[
    ds = new org.mule.message.ds.StringDataSource('key1','value1','text/plain');
    dh = new javax.activation.DataHandler(ds);
    message.outboundAttachments['key1'] = dh;

    ds = new org.mule.message.ds.StringDataSource('key2','value2','text/plain');
    dh = new javax.activation.DataHandler(ds);
    message.outboundAttachments['key2'] = dh;
]]></expression-component>

<set-payload value="#[null]" />

<http:outbound-endpoint exchange-pattern="request-response"
    method="POST" address="http://localhost:8082/path" />

Be careful to not specify a value for contentType otherwise Mule will not be able to provide the multi-part boundary in the POST.

If you happen to have a Content-Type message property in the outbound scope, remove it with:

<remove-property propertyName="Content-Type" />

before the HTTP outbound endpoint.




回答2:


Only to complement i'm using Anypoint Studio 3.5.2 with mule runtime 3.5.0, the signature of the constructor of the class org.mule.message.ds.StringDataSource is different, rather than:

org.mule.message.ds.StringDataSource ('key1 '' value1 ',' text / plain ');

is

org.mule.message.ds.StringDataSource ('value1', 'key1', 'text / plain');



来源:https://stackoverflow.com/questions/20202014/mule-httpoutbound-endpoint-multipart-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!