file-uploadmultipartform-dataquarkusresteasyrest-client

Quarkus RestClient doesn't forward multipart file uploads—files received count is 0


I am working with a Quarkus-based service that accepts multiple file uploads and forwards them to another (Quarkus)service via a RestClient. The issue I am facing is that, although the files are uploaded successfully to the initial service, the receiving service logs show that the files count is always 0. This happens when the files are forwarded via the RestClient. If I upload the files directly via Postman, the receiving service works as expected.

Here’s a breakdown of the code and the issue I’m encountering:

Sending Service: The sending service has an endpoint that receives multiple files via multipart form data and then forwards them to another service using a RestClient.

Endpoint:

@POST
@Path("/uploadFiles")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("application/json")
public Response uploadFiles(@RestForm("files") List<FileUpload> fileUploads) {
    log.info("Number of files uploaded: " + fileUploads.size());  // Correctly logs the number of uploaded files
    String restClientResponse = myService.uploadFiles(fileUploads);
    return Response.ok(restClientResponse).build();
}

Service Method:

public String uploadFiles(List<FileUpload> fileUploads) {
    MultipartFormDataOutput multipartFormDataOutput = new MultipartFormDataOutput();

    for (FileUpload fileUpload : fileUploads) {
        File file = fileUpload.uploadedFile().toFile();  // Get the actual file object
        multipartFormDataOutput.addFormData("files", file, MediaType.APPLICATION_OCTET_STREAM_TYPE, file.getName());
    }

    Response response = externalClient.uploadFiles(multipartFormDataOutput);

    if (response.getStatus() != 200) {
        throw new RuntimeException("Failed to upload files. Status: " + response.getStatus());
    }

    return response.readEntity(String.class);
}

RestClient:

@POST
@Path("/uploadFiles")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
Response uploadFiles(@MultipartForm MultipartFormDataOutput data);

Receiving Service (Quarkus): The receiving service is supposed to accept the multipart file upload and process it. But my restClient is not sending it correctly

Endpoint:

@POST
@Path("/uploadFiles")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("application/json")
public Response uploadFiles(@RestForm("files") List<FileUpload> fileUploads) {
    log.info("Number of files uploaded: " + fileUploads.size());  // Always logs 0 when forwarded via RestClient

    for (FileUpload fileUpload : fileUploads) {
        String fileName = fileUpload.fileName();
    }

    return Response.ok("Files uploaded successfully").build();
}

The Problem:

When files are uploaded directly via Postman, the receiving service correctly logs the number of files and processes them as expected.

When the same files are forwarded via the RestClient, the receiving service logs that 0 files were received.

It seems like the multipart form data is not being properly constructed or forwarded through the RestClient.

What am I missing? How can I ensure that the files are correctly forwarded and processed by the receiving service when using a RestClient, so I get both the file and the metadata like file name?

Sample project to test: GitHub project to test


Solution

  • The problem is that in your code the REST Client is using org.jboss.resteasy.reactive.server.multipart.MultipartFormDataOutput.

    That type is not supported by the client, it's strictly a server type.

    What you need to do is use org.jboss.resteasy.reactive.client.api.ClientMultipartForm.

    An example of how to use it can be found here.