2
votes

i am using a http outbound endpoint to call a separate service that needs to process a file that is sent to mule in a multipart request. Here is an example

<http:outbound-endpoint connector-ref="serviceConnector"
                address="http://${serviceHost}:${servicePort}/upload"
                method="POST"
                responseTimeout="${endpointTimeout}"
                mimeType="multipart/form-data">

The problem i am having is that when this service is called i am getting a FileUploadException saying that the request was reject because no multipart boundary has been found.

I have been experimenting with this problem and looking at different questions but none of them seems to resolve the issue.

Mule ESB and "multipart/form-data"

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

https://www.mulesoft.org/jira/browse/MULE-6917

I have also tried altering the connector like explained in this question: Send file to Mule inbound-endpoint but no luck.

Any ideas?

Thanks

2

2 Answers

2
votes

Finally this problem was solved by creating a custom processor that was parsing the incoming request, splitting it into parts and manually reading the bytes of the parts input stream and setting the read bytes as a data source that is attached to the message. The code:

 InputStream in = message.getPayload( InputStream.class );
 MultiPartInputStream multiIn = new MultiPartInputStream( in, contentType, null );
try
    {
        Collection<Part> parts = multiIn.getParts();

        for ( Part part : parts )
        {
           if ( part instanceof MultiPart )
                {
                    is = part.getInputStream();
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                    byte[] isByteArray = new byte[1024];
                    int numberRead;
                    while ( (numberRead = is.read( isByteArray ) ) != -1 ){
                        buffer.write(isByteArray, 0, numberRead);
                    }
                    byte[] bytesRead = buffer.toByteArray();
                    DataHandler attachment = new DataHandler(new ByteArrayDataSource( bytesRead, "application/octet-stream")); 
                    message.addOutboundAttachment( part.getName(), attachment );
                }
         }
   }...handle exceptions etc...

Hopefully this helps someone!

0
votes

You don't show enough of your flow to really be sure about what's missing but at first glance, I'd say you are not setting the boundary attribute in the outbound request Content-Type header.

Read the following to better understand how multipart/form-data requests work: http://chxo.com/be2/20050724_93bf.html