#Header filter

4 messages · Page 1 of 1 (latest)

charred lintel
#

Guys when i set my webclient in bean

@Bean
    public WebClient webClient() {
        return WebClient.builder()
                        .baseUrl("https://api.github.com")
                        .build();

    }

and wrote filter to check if there's “Accept: application/json” header
this filter doesnt really intercept it

@Configuration
@Component
public class WebFilterHeaders implements WebFilter {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        var acceptHeader = exchange.getRequest().getHeaders().getFirst("Accept");

        if (acceptHeader == null || !acceptHeader.equals("application/json")) {
            exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }
}

Here's examplary requests call

return webClient.get()
                 .uri(uriBuilder -> uriBuilder
                 .path("/users/{request}/repos")
                 .build(request.nickname()))
                 .exchangeToFlux(res -> res.bodyToFlux(RepositoryData.class))
                 .filter(repositoryData -> !repositoryData.fork())
                 .flatMap(repositoryData -> getBranchesAndSHAs(repositoryData)
                 .collectList()
                 .map(branchDataList -> new GetRepoResponse(repositoryData.name(), repositoryData.owner().login(), branchDataList)))
                 .collectList();

Does anyone know where the problem is? I didn't set it default header “Accept: application/json”
on purpose to check if filter works correctly and it seems dont

uneven berryBOT
#

This post has been reserved for your question.

Hey @charred lintel! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

wet lodge
#

Please show the full java code including the imports. I am not sure what WebFilter you are implementing.