Tim
Tim

Reputation: 4284

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

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?

Upvotes: 8

Views: 1420

Answers (1)

Alfredo
Alfredo

Reputation: 31

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.

Upvotes: 1

Related Questions