multipartform-dataresteasymultipartquarkus

Handle multipart/form-data with Quarkus


I'm facing an issue, I can't get my form in my resources, the variables are always null

My Resource :

    @POST
    @Path("/upload-logo")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    public String uploadLogo (@MultipartForm LogoMultipartForm logoMultipartForm) throws IOException {
        return this.companyService.uploadLogo(username, logoMultipartForm.logo);

    }

The form model

public class LogoMultipartForm {

    @FormParam("logo")
    public byte[] logo;

    @FormParam("filename")
    @PartType("text/plain")
    public String fileName;
}

My Fetch request :

  uploadLogo: async (file: File) => {
    const form = new FormData();
    form.append("logo", file, "logo.png");
    form.append("filename", "test");

    const { query, abort } = HttpClient.POST(`${COMPANY_URL}/upload-logo`, form);
    let promise = query
      .then((res: any) => {
        console.log("Response", res);
        if (res.status === 200) {
          return res.text();
        } else {
          throw res;
        }
      })
      .then((url: any) => url);

    promise.cancel = abort;

    return promise;
  },

And my HttpClient :

  POST: function (url: string, body: any, config?: any) {
    const controller = new AbortController();
    const signal = controller.signal;
    return { query: fetch(url, { signal, method: "POST", body, ...config }) as any, abort: () => controller.abort() };
  },

To be sure I was testing with a proxy and the request is effectively good : The variable fileName and logo is always null.

This is my pom.xml :

  <dependencies>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-smallrye-graphql</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-flyway</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-jdbc-postgresql</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-hibernate-orm-panache</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-reactive-pg-client</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-qute</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-oidc</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-rest-client</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-multipart</artifactId>
    </dependency>

  </dependencies>

I don't see where is the problem.

I have already seen the official resteasy documentation for Multipart and don't see where is the problem. I have already Tested with MultipartFormDataInput and all parts are empty.

Thanks in advance for your help ! :)


Solution

  • You are missing the dependency:

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-multipart-provider</artifactId>
    </dependency>
    

    Be sure that your @PartType("text/plain") inherits from org.jboss.resteasy package! As well as the @MultipartForm (import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;)

    There is a good example how it works: Quarkus tutorial. Look at the packages!