quarkusquarkus-rest-clientquarkus-reactive

How can I use FileUpload in a Quarkus RestClient to upload file to a external rest service?


I'm using org.jboss.resteasy.reactive.multipart.FileUpload in a server endpoint, and it is working well.

Now I need to send a generated file to an external Rest service.

I found org.jboss.resteasy.reactive.server.core.multipart.DefaultFileUpload, but it need org.jboss.resteasy.reactive.server.multipart.FormValue as parameter, and a non-public implementation.

I'm supposed to create a class implementing that interface or am I missing something easier ?


Solution

  • Let's assume that the server receives a multipart request that looks like this:

    public class ServerRequest { 
    
      @RestForm("files")
      @PartType(MediaType.APPLICATION_OCTET_STREAM)
      List<FileUpload> files;
    
      @RestForm("jsonPayload")
      @PartType(MediaType.TEXT_PLAIN)
      String jsonPayload;
    }
    

    Then you can create a client request like this:

    public ClientMultipartForm buildClientMultipartForm(ServerRequest request) { 
      ClientMultipartForm multiPartForm = ClientMultipartForm.create();
      multiPartForm.attribute("jsonPayload", request.getJsonPayload(), "jsonPayload"); 
      request.getFiles().forEach(fu -> {
        multiPartForm.fileUpload(fu); 
      });
      return multiPartForm;
    }
    

    More details can be found https://quarkus.io/guides/rest-client#converting-a-received-multipart-object-into-a-client-request