Search code examples
javaspringoauth-2.0webclient

WebClient returning null after making a request to the reddit api


I am trying to write a backend to integrate with reddits API, I created a web client with my app clientId and secret which works fine for this request

webClient.get()
            .uri("/api/v1/me")
            .attributes(ServerOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId("reddit"))
            .retrieve()
            .bodyToMono(new ParameterizedTypeReference<String>() { })
            .block();

this returns a valid response as a String, since I have been testing my implementation I haven't introduced any models yet; on the other side the request that I make to retrieve a random post from /r/theonion subreddit returns null without any exceptions.

webClient.get()
            .uri("/r/theonion/random")
            .attributes(ServerOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId("reddit"))
            .retrieve()
            .bodyToMono(new ParameterizedTypeReference<String>() { })
            .block();

This request just returns null and I have no idea what is wrong with it since I am not getting any exceptions and the similar request(the one above) is working fine. Also after authenticating myself with postman using the app clientId and secret, I can execute this call and get a response. enter image description here Any help would be appreciated! Thank you!


Solution

  • I found out the reason, it turns out the random endpoint for Reddit returns a 302 redirect which redirects to a real request to retrieve a random post so enabling the redirect for WebClient solved the issue for me. I used netty HttpClient to enable the redirects so adding this solved the issue for me:

    .clientConnector(new ReactorClientHttpConnector( HttpClient.create().followRedirect(true)))