2

I am currently using the Java MicroProfile RestClient and have following problem:

  • Backend provides an api to receive binary files
  • Backend would be happy to receive a Content-Type header containing the Mime-Type of the binary file
  • I am not able to set Content-Type per method parameter

I have following code on client-side:

import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import io.smallrye.mutiny.Uni;

@RegisterRestClient
public interface RestClient {
    @POST
    @Path("/api/path/v1")
    Uni<String> createResource(@HeaderParam("Content-Type") String contentType, byte[] body);
}

The @HeaderParam("Content-Type") will always be overwritten with "application/json".

If I set the @Consumes Property the Content-Type would always be the same but I want to set it during the method call. (byte[] could contain image, video, text ...)

Has anyone an idea how I could archive this? May there is a better option instead of using a simple byte[] as body?

Best thanks!

Sevenby7
  • 21
  • 2
  • Have you tried using a `ClientRequestFilter`? – geoand Aug 02 '22 at 11:27
  • @geonand have you a suggestion how this could work?I have worked with ClientRequestFilters in the past to overwrite HTTP Headers but in this case I need to set a specific Content-Type per method call. I could add an Header like "ContentType" use this in ClientRequestFilter and set the value afterwards to "Content-Type" Header but this seems to be hacky. – Sevenby7 Aug 03 '22 at 07:29
  • @geoand have tried, Content-Type will be still overwritten. Have tried is as following: – Sevenby7 Aug 05 '22 at 08:50
  • ''' import javax.inject.Inject; import javax.ws.rs.client.ClientRequestContext; import javax.ws.rs.client.ClientRequestFilter; import javax.ws.rs.ext.Provider; import io.vertx.core.http.HttpServerRequest; @ Provider public class MyFilter implements ClientRequestFilter { @ Inject HttpServerRequest httpServerRequest; @ Override public void filter(ClientRequestContext requestContext) { httpServerRequest.headers().remove("Content-Type"); httpServerRequest.headers().set("Content-Type", "image/jpg"); } } ''' – Sevenby7 Aug 05 '22 at 08:52
  • If I set an other http header like "sample" it will be added to the request – Sevenby7 Aug 05 '22 at 08:52
  • @geoand do you have any other idea how to solve this problem? – João Pedro Schmitt Oct 13 '22 at 12:56

1 Answers1

0

i just found the solution for this question.

You need to set annotation @Consumes(MediaType.MEDIA_TYPE_WILDCARD)

@POST
@Consumes(MediaType.MEDIA_TYPE_WILDCARD)
Response post(@Nullable Object bodyRq);

then set the Content-Type while register header

 var call = RestClientBuilder.newBuilder()
            .baseUri(new URI(uri))
            .register(new HeaderAddingFilter(headersRq)) // Content-Type: Application/xml for example
            .build(GenericOperation.class);

so then the resteasy-client will sent the content-type header following the value set on headerRq.

hope this help other who facing same usecase.

HermanW
  • 19
  • 1
  • 2