0

I am attempting to setup a REST API on a Wildfly App Server that accepts file uploads. While testing it I came across the following issue. When attempting to upload using Content-Type: multipart/form-data I am receiving the following response:

HTTP Status Code: 400
Content-Type: text/html;charset=UTF-8
Body: "java.io.IOException: RESTEASY007550: Unable to get boundary for multipart"

This is the attempted request.

Headers 
{
  "content-length": "233",
  "content-type": "multipart/form-data",
  "accept-encoding": "multipart/form-data",
  "authorization": "Bearer ommitted"
  "user-agent": "PostmanRuntime/7.28.3"
}

Body
----------------------------976685076323434093219932
Content-Disposition: form-data; name="file"; filename="import.csv"
Content-Type: text/csv

destination
1234567890

----------------------------976685076323434093219932--

The API endpoint is configured as following:

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;

@Stateless
@Path("/import")
@Produces(MediaType.APPLICATION_JSON)
public class ImportAPI {

    @POST
    @Path("/{id}/do")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response doImport(
        @Context HttpServletRequest request,
        @PathParam("id") Integer campaignId,
        MultipartFormDataInput input) {
        // Code omitted
        return Response.ok().build();
    }
}

There is no error log printed on server log when the error occurs. I am using Wildfly 23 and RESTEasy 3.15.1.Final (provided by Wildfly App Server).

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-multipart-provider</artifactId>
    <version>3.15.1.Final</version>
    <scope>provided</scope>
</dependency>

I am not entirely sure if it's something wrong in the request or in the API endpoint and I am looking to shed some light on it.

Haris
  • 138
  • 1
  • 11

1 Answers1

1

The Content-Type field is wrong. In your example it needs to be something like:

"Content-Type": multipart/form-data; boundary="--------------------------976685076323434093219932"

There are two fewer minus (dash) characters than are shown in the current body of your example.

stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • Which Content-Type is wrong? To my understanding there are two references of Content-Type, the one set as header on the request and one for each part included in the multipart. The one set on the request is already multipart/form-data. Do I need to set the same Content-Type for each part in my request? – Haris Sep 29 '21 at 16:49
  • As I show it's the "overall" content type. You need to tell the post that the body is "multipart/form-data" and what the boundary is. Each part will then have it's own content-type. – stdunbar Sep 29 '21 at 16:51