How send a file in an HTTP request and upload it t

2019-03-04 12:44发布

问题:

I want to send a file in a HTTP POST request and then have Mule upload the file to a file directory on a server using FTP. It looks like the FTP connector saves the payload to the file directory but this is coming through as null and an empty file is written to the directory by FTP.

I've had some success with binary/octet stream as the media type in the raml and http request using Postman but the file doesn't open and looks like it may be corrupted and when I use form-data as the media type in the raml and http request it shows as a key value pair in message.inboundAttachments but how do I get the FTP connector to write from inboundAttachments. I have tried setting it as the payload which shows as an javax object but has problems when writing it to the file directory. How can I do this and what is the best approach to take? Files would be files >= 1 and could be different file types. I'm currently just testing it with one file.

Also, when the file is written is is named 28f42420-b325-11e7-8ffb-dcb320524153.dat. How can I specify the name as it looks like message.inboundProperties.originalFilename is null?

I'm using Anypoint Studio 6.2 and Mule 3.8.3 and Postman to make the http calls as Mule console does not appear to give the option to send a file in the request.

RAML

  /ftp:
    displayName: FTP Test 
    description: Send file to directory using FTP
    post:
      description: Send file
      body:
        binary/octet-stream:
        multipart/form-data:
        application/x-www-form-urlencoded:
          properties:
            file:
              description: The file to be uploaded
              required: true
              type: file
      responses:
        200:
          body:
            application/json:

Current XML flow

<flow name="sendFtpFile">
    <set-payload value="#[message.inboundAttachments.file]" doc:name="Set Payload"/>
    <ftp:outbound-endpoint host=${host} port="21" path=${filePath} user=${user} password=${pwd} connector-ref="FTP" responseTimeout="10000" doc:name="FTP"/>
</flow>  

Thanks

回答1:

RAML to send multiple files to Mule api/endpoint could be defined as mentioned in answer to the post below-

Send multiple Files in HTTP request using RAML

To read these files in Mule api,you can use a for-each construct,something like below could be useful:

<foreach doc:name="For Each" collection="#[message.inboundAttachments]">
       <set-payload value="#[payload.dataSource.content]" doc:name="Set Payload"/>
       <byte-array-to-string-transformer doc:name="Byte Array to String"/>
       <logger message="Content :- #[payload]" level="INFO" doc:name="Logger"/>

</foreach>

Once read you can edit or transform the file as required and add it as attachment using set-attachment

<set-attachment attachmentName="requiredFile.xml" value="#[payload]" contentType="text/xml" doc:name="Attachment"/>

And then you can send it to required FTP connector.