spring-bootattributesbad-requestwebtestclient

How to add request attributes to a WebTestClient / WebClient?


I looked around but it seems as if there is no suitable solution for a problem I am not alone with: Why am I not able to add attributes to a WebClient (or WebTestClient respectively) request? Whenever I try something like:

var respSpec = webTestClient.post()
        .uri("http://localhost:10080" + "/endpoint")
        .header(apiSecretHeader, secret)
        .header("HEADER_1", "header value 1")
        .header("HEADER_2", "header value 2")
        .attribute("requestId",  UUID.randomUUID().toString())
        .attribute("aUrl", theUrl)   // private URL theUrl;
  //    .attributes(m())
        .exchange();

I get a Bad Request (400) as response. Trying to use .attributes instead of .attribute results in a 400 either.

The REST controller's endpoint:

@PostMapping(value = "/endpoint", produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseStatus(HttpStatus.ACCEPTED)
    public ResponseEntity<?> triggerAction(@RequestAttribute final HttpHeaders httpHeaders, @RequestAttribute final String requestId, @NonNull @RequestAttribute(value = "myActionUrl") final URL myActionUrl) {
    ...
}

Console output:

2021-11-19 09:06:08.050 ERROR [main] [] [] [VNB_REST_OUTBOUND_ADAPTER] [system] [Initiale Log Konfiguration] o.s.t.w.r.server.ExchangeResult:231 - Request details for assertion failure:

> POST http://localhost:10080/endpoint
> WebTestClient-Request-Id: [1]
> X-SECRET: [secret]
> HEADER1: [header value 1]
> HEADER2: [header value 2]

No content

< 400 BAD_REQUEST Bad Request
< Content-Type: [application/json]
< Transfer-Encoding: [chunked]
< Date: [Fri, 19 Nov 2021 09:06:07 GMT]
< Connection: [close]

{
  "timestamp" : "2021-11-19T09:06:07.963+00:00",
  "status" : 400,
  "error" : "Bad Request",
  "path" : "/endpoint"
}
 

java.lang.AssertionError: Status expected:<204 NO_CONTENT> but was:<400 BAD_REQUEST>
Expected :204 NO_CONTENT
Actual   :400 BAD_REQUEST

You can see that the attributes I tried to add seems not to be considered.


Solution

  • Assuming you are talking about request parameters the simplest way is using WebClient.Builder as follows:

    var uriTemplate = "http://localhost:10080/endpoint?requestId={requestId}&aUrl={aUrl}";
    var respSpec = webClientBuilder.baseUr(uriTemplate).build()
          .post()
          .uri( uri -> uri.build(UUID.randomUUID(), theUrl) )
          .header(apiSecretHeader, secret)
          .header("HEADER_1", "header value 1")
          .header("HEADER_2", "header value 2")
          .exchange();
    

    If you prefer to not use the URI template, you would need to specify everything in the URI as follows:

    var respSpec = webTestClient.post()
          .uri( uriBuilder - > uriBuilder
            .scheme("http")
            .host("localhost")
            .port("10080")
            .path("/endpoint")
            .queryParam("requestId", requestId)
            .queryParam("aUrl", aUrl)
            .build())
          .header(apiSecretHeader, secret)
          .header("HEADER_1", "header value 1")
          .header("HEADER_2", "header value 2")
          .exchange();