How to consume File with a content type of message/rfc822 in my REST API in a MultipartFormData?
Asked Answered
A

1

8

In my Vue App I have this file input:

<input type="file" multiple @input="handleFileInput" />

The selected files are passed to my this upload method:

 public async uploadFiles(file: File) {

    [...]
    const formData = new FormData();
    formData.append('fileName', file.name);
    formData.append('file', file);
    [...]
    await axios.post('my-upload-url', formData);    
}

The js log before sending it:

enter image description here

The network log after sending it:

enter image description here

My Resteasy controller answers:

@POST
@Transactional
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadMultipartData(@MultipartForm @Valid MyDto dto) {
 // dto.file -> my file
}

The DTO:

public class MyDto {
    @NotNull
    @FormParam("file")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    public InputStream file;
}

When transferring other file types (pdf, png, jpg, mp3, ...) the file is in the InputStream of the DTO as expected. However, when it the file is an .eml file, the InputStream is null.

Update: when renaming the file from text.eml to test.txt, the upload works.

Why?

In other words, how do I consume a File with a content type of message/rfc822 in my REST Api? Do I need a separate method that accepts this specific Media Type?

Albertype answered 18/11, 2021 at 19:17 Comment(5)
You declare @PartType(MediaType.APPLICATION_OCTET_STREAM) and then give something that's not an octet stream. Mime type should reflect what you are actually uploading.Tupungato
Are you using RESTEasy or RESTEasy Reactive?Puppetry
I am using RESTEasyAlbertype
@Tupungato so in order to upload any file, I need one annotated Method with every possible Mime Type?Albertype
Is there any feedback on this? I have the same problem. General purpose upload form. Can upload png and other stuff, but eml crashes in the resteasy server (quarkus) before reaching my code.Fantast
U
1

So this most certainly has to do with how your server handles the uploaded file. application/octet-stream simply represents a generic/unknown binary file. From RFC 2046 § 4.5.1:

The "octet-stream" subtype is used to indicate that a body contains arbitrary binary data.

Most apps will try to determine the file mimetype by looking at their file's extension, but it will have trouble determine it if the extension is unknown to them. An eml file is just a text file, which explains why when changing the extension to txt works without problems. The easiest way to verify this by taking a simple txt file and changing its extension to unknown (for example) and try to upload that file.

I have set up an example replicating the exact same scenario and in this case the server has no trouble reading the file contents.

Ushijima answered 21/5, 2022 at 3:45 Comment(1)
Thanks - but how do I consume the .eml? It does not make it to my controller 🤔Albertype

© 2022 - 2024 — McMap. All rights reserved.