I use micronaut and kotlin. So I trying to add header to request to other service using interceptor. Can you give me example?
So I trying to add header to request to other service using interceptor. Can you give me example?
Here is an example from https://docs.micronaut.io/latest/guide/index.html#clientFilter:
@Filter("/repos/**")
class BintrayFilter implements HttpClientFilter {
final String username;
final String token;
BintrayFilter(
@Value("${bintray.username}") String username,
@Value("${bintray.token}") String token ) {
this.username = username;
this.token = token;
}
@Override
public Publisher<? extends HttpResponse<?>> doFilter(MutableHttpRequest<?> request, ClientFilterChain chain) {
return chain.proceed(
request.basicAuth(username, token)
);
}
}
In the doFilter
method there you can do whatever you like with the request, including adding headers to it.
EDIT
The question indicates Kotlin is being used. Here is the Kotlin counterpart which is available at the same url as lined above:
@Filter("/repos/**")
internal class BintrayFilter(
@param:Value("\${bintray.username}") val username: String,
@param:Value("\${bintray.token}") val token: String) : HttpClientFilter {
override fun doFilter(request: MutableHttpRequest<*>, chain: ClientFilterChain): Publisher<out HttpResponse<*>> {
return chain.proceed(
request.basicAuth(username, token)
)
}
}