0

Using Mule 4.x, I would like to have my API set up with an HTTP POST method endpoint, that allows a user to post form data containing a file.

Then I would like to take that file, and e-mail it with the posted file as an attachment on the e-mail.

This should be file-type agnostic if possible, as it could be any filetype that is posted to the API.

I can't seem to transform the form part's "content" into something that is suitable for an e-mail attachment.

I've gone through many different iterations. What I've got at this very moment.. When I log the payload, I get:

 ----------------------------344843990838395406613242 
Content-Disposition: form-data; name="fileContents"; filename="upload.txt" 
Content-Type: file

[{"message": "Hello World!"}]
----------------------------344843990838395406613242--

Which looks good. I can access the filename and log it by using: #[payload.parts[0].headers.'Content-Disposition'.filename]

But whenever I try to access: #[payload.parts[0].content]

Whether it is to attempt to 'log' it, or in the Attachment field of the Send Email component, I get the following Error message:

""Unable to find a sub type in `file, while writing Weave.
Trace: at main (Unknown)" evaluating expression: "payload.parts[0].content"."

Separately, I was able to attach a local file to an e-mail, by using a 'Read' component, and then a 'Set Variable' to assign it to a variable, with the MIME type of "application/binhex". Then I appeared to be able to attach any file type to the e-mail just by changing the local file that was being pointed to.

But I'm not sure how to make the connection from the incoming form-part file, to attaching it.

I feel like there must be a step I'm missing. Any tips/tricks/advice/examples would be greatly appreciated. Do I need to 'read' the content stream and transform it into something?

Note: Also, there is no specific reason that I am trying to use a POST with the form-data body containing the file. That just seemed to make the most sense to me, but if there is a better way to send it that would work better, by all means, let me know.

Edit/Additional Info: In the video here https://www.youtube.com/watch?v=5_YKXE8E7Sw I noticed that his payload outputs the correct Content-Type, not just "file" like mine does. The only difference that I noticed is that I am using APIKit as the entry point into my API. He was not.

I created a test project just using a standard http listener, and the output does indeed contain the actual Content-Type of the file.

Maybe this is a bug in APIKit? Or do I need to configure something specifically to allow the actual file input type to come through correctly?

Topher
  • 7
  • 4
  • You should not try to log the attachment. Focus on the error sending instead. Please share the transformation and email send operation of your flow as text XML, not screenshots. Also the complete log of the error sending, not just the message. – aled Mar 30 '22 at 21:38
  • Note that there is no "Using Mulesoft 4.x". MuleSoft is a company name. Mule 4 is the product. You are using the product. – aled Mar 30 '22 at 21:40
  • I do apologize for any confusion that caused you. I am using Mulesoft's Anypoint Studio 7.x to create a **Mule** Application that will run in the **Mule** 4.x Runtime Environment. – Topher Mar 31 '22 at 13:24
  • I've updated my original question with new information. It seems that my use of APIKit might be affecting the functionality. – Topher Mar 31 '22 at 14:29
  • Hi @Topher, your question should include the flow as text (XML), so others can search and reproduce the issue. It is possible that APIKit usage is impacting the behavior. We could have seen it in the flows/configurations if shared. Also indicate exact versions of APIKit and Mule 4 used. See https://stackoverflow.com/help/how-to-ask for more tips on how to improve your question. Usage of screenshots, sharing of text code and errors, help to reproduce are already mentioned. – aled Mar 31 '22 at 14:46
  • Thanks for the tips @aled. I intended to add such things when time allowed. As I'm working on a production application, it was not easy for me to just copy and paste code from the project. I would have had to create a sample project first to reproduce it, so that I wasn't sharing anything confidential. – Topher Mar 31 '22 at 16:52
  • I understand but remember that Stackoverflow.com is a questions&answers site. Responses will depend of the quality of the question. – aled Mar 31 '22 at 19:41

1 Answers1

0

I've found a solution with the help of someone from the Mulesoft forums.

Rather than posting my file as a form part data, I am Base64 encoding the contents of my file and sending it as a JSON formatted body of the http post. Then using this json payload to transform into an e-mail attachment to add to the E-mail Connector.

This seems to be working perfectly. And seems to be file-type agnostic.

Topher
  • 7
  • 4