1

I am trying to get a header value from the config into the Rest(easy) client using the @ClientHeaderParam annotation as described here https://quarkus.io/guides/rest-client-reactive#custom-headers-support, unfortunately it does not work out. The value is sent as-is, rather than replaced with the corresponding config property Here is roughly what I am doing

@RegisterRestClient
@ClientHeaderParam(name = "Key", value = "${api-key}")
public interface MyClient {

  @POST
  @Path("/api")
  @Consumes(MediaType.APPLICATION_OCTET_STREAM)
  @Produces(MediaType.APPLICATION_JSON)
  Response call(InputStream image);
}

When I invoke the call method and check the request, I see that the Key header has ${api-key} as value, and not the value I have in application.properties for api-key.

Thanks in advance.

zakaria amine
  • 3,412
  • 2
  • 20
  • 35

2 Answers2

2

As per the documentation of the microprofiles, the annotation ClientHeaderParam does not support reading values from config. Instead we can provide the default method or static method from some sort of utility class. Please refer to the javadoc at https://download.eclipse.org/microprofile/microprofile-rest-client-1.2.1/apidocs/org/eclipse/microprofile/rest/client/annotation/ClientHeaderParam.html

Following is sample code that might be of use in your context:

@RegisterRestClient(baseUri = "http://localhost:8000")
@ClientHeaderParam(name = "Key", value = "{getApiKey}")
@ClientHeaderParam(name = "api-key", value = "{getConfigValue}")
public interface MyRemoteService {

    default String getApiKey() {
        return ConfigProvider.getConfig().getValue("api-key",String.class);
    }

    default String getConfigValue(String key) {
        return ConfigProvider.getConfig().getValue(key,String.class);
    }

    @GET
    @Path("/hello")
    @Produces(MediaType.TEXT_PLAIN)
    String helloWithKeyHeader();
}

Refer to sample code at https://github.com/gopinnath/quarkus-rest-example-parent

  • It works thanks! but still it does not answer the question why the value from the config is not picked up. Yes, in microfprofile javadoc, there is no mention, but according to quarkus docs, it should work. – zakaria amine Mar 18 '22 at 17:49
  • 1
    I had update my code at [repo](https://github.com/gopinnath/quarkus-rest-example-parent) with similar example as quarkus docs. The solution is to implement ClientHeadersFactory that can process the header before sending. – Gopinath Radhakrishnan Mar 19 '22 at 04:04
0

Based on the answer I got on github https://github.com/quarkusio/quarkus/discussions/24418

The issue is related using the wrong dependency. I should be using quarkus-rest-client-reactive while I am using quarkus-rest-client. This is actually weird because it has nothing to do with reactive or not reactive, but it is the way it is.

zakaria amine
  • 3,412
  • 2
  • 20
  • 35