I am using WebClient to access Endpoints, which are simple echo Endpoints, to test the connection. Everytime I am calling the WebClient get method I get a 401 error, but when I try to access the Endpoints with Swagger with the same credentials it works without any problems. I'm pretty sure I forgot something simple, but looking at different tutorials it seems like I've done everything correctly.
My Code:
try{
WebClient webClient = WebClient
.builder()
.baseUrl("http://localhost:8030")
.defaultHeaders(headers -> headers.setBasicAuth("admin","adminPassword"))
.build();
String getValue = webClient.get()
.uri(uriBuilder -> uriBuilder
.path("/echo")
.queryParam("inputString", "{input}")
.build("get test"))
.retrieve()
.bodyToMono(String.class)
.block();
System.out.println(getValue);
}catch (WebClientResponseException ex){
System.out.println("WebClientResponseException code: " + ex.getStatusCode() +"\n"+"WebClientResponseMessage: " + ex.getResponseBodyAsString());
throw ex;
}
This is the printed error I get:
WebClientResponseException code: 401 UNAUTHORIZED
WebClientResponseMessage: <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>401 Unauthorized</title>
<meta name="robots" content="noindex,nofollow"/>
</head>
<body>
<h1>401 Unauthorized</h1>
</body>
</html>