Micronaut Httpclient wont accept string for Multipart request to upload file

27 Views Asked by At

I have a task to send multipart request with file part and metadata part(as json metadata with content type). I am using Micronaut framework and its HttpClient.

I see we cannot provided metadata as json with content-type as application/json. So I created the http requestBody on my own as below.

--2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f
Content-Disposition: form-data; name="file"; filename="sample_file.jpeg"
Content-Type: application/octet-stream

<PNG image byte>
--2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f
Content-Disposition: form-data; name="metadata";
Content-Type: application/json

{"traceId":"69e8708e-4532-4236-8cc6-741f8e6e1957", bla bla bla.....}
--2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f--

Now I am trying to send it thorugh httpclient as below, but this is failing.

HttpRequest<?> request =
        HttpRequest.create( <url> )
            .body(new String(requestBody))
            .header(AUTHORIZATION,
                "Bearer ")
            .header(HttpHeaders.CONTENT_TYPE, "multipart/form-data;boundary=2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f");
    String responseBody = httpClient.toBlocking().retrieve(request);

But this call is failing with below error:

io.micronaut.http.multipart.MultipartException: The type java.lang.String is not a supported type for a multipart request body

How to resolve this issue any idea.

0

There are 0 best solutions below