Spring WebFlux WebClient - How to resolve 400 Bad Request
Asked Answered
A

2

6

I am a newbie to reactive programming and I am using Spring WebFlux's WebClient to make a POST request to the below URL as part of my Spring Boot application to assign an existing quiz to a candidate. I am having trouble understanding what I've done wrong in constructing my WebClient request.

End-point

https://www.flexiquiz.com/api/v1/users/{user_id}/quizzes

In my request body, I need to pass the quiz id that I get from another API (works fine).

{
   "quiz_id": ""
}

Apart from passing the request body, I am also passing X-API-KEY as part of request header.

However, when I tried hitting the end-point, I am getting a {"message":"400: Bad Request"} error.

Below is my code.

QuizRequest.java

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class QuizRequest {

  @JsonProperty("quiz_id")
  @NotBlank
  private String quizId;

  public QuizRequest(@NotBlank String quizId) {
    this.quizId = quizId;
  }
}

FlexiQuizClient.java

@Service
@Slf4j
public class FlexiQuizClient {

  private static final String USER_AGENT = "WebClient for FlexiQuiz";

  private final WebClient webClient;

  @Value("${flexiquiz.baseurl}")
  private String FLEXIQUIZ_API_BASE_URL;

  @Value("${flexiquiz.key}")
  private String FLEXIQUIZ_API_KEY;

  @Autowired
  public FlexiQuizClient() {
    this.webClient = WebClient.builder()
        .baseUrl(FLEXIQUIZ_API_BASE_URL)
        .defaultHeader(HttpHeaders.USER_AGENT, USER_AGENT)
        .filter(logRequest())
        .build();
  }

  public String assignQuizToCandidate(String userId, QuizRequest quizRequest) {
    return Objects.requireNonNull(webClient.post()
        .uri(FLEXIQUIZ_API_BASE_URL + "/v1/users/" + userId + "/quizzes")
        .header("X-API-KEY", FLEXIQUIZ_API_KEY)
        .body(Mono.just(quizRequest), QuizRequest.class)
        .exchange()
        .block())
        .bodyToMono(String.class)
        .block();
  }

  private ExchangeFilterFunction logRequest() {
    return (clientRequest, next) -> {
      log.info("Request: {} {}", clientRequest.method(), clientRequest.url());
      clientRequest.headers()
          .forEach((name, values) -> values.forEach(value -> log.info("{}={}", name, value)));
      return next.exchange(clientRequest);
    };
  }
}

In my resource class (controller), I am calling the web client method as shown below:

ResponseResource.java

private String assignQuizToCandidate(String userId, QuizRequest quizRequest)
      throws ParseException {
    log.info("Assigning a quiz based on your skill");
    String message = quizClient.assignQuizToCandidate(userId, quizRequest);
    log.info("message from status: " + message);
    JSONParser parser = new JSONParser();
    JSONObject json = (JSONObject) parser.parse(message);
    return (String) json.get("message");
}

Inside another method I am calling the above method as shown below.

QuizRequest quizRequest = new QuizRequest(openQuiz.get().getQuizId());
String status = assignQuizToCandidate(userId, quizRequest);

Below are the logs:

2020-05-17 10:20:09.938 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Channel acquired, now 1 active connections and 0 inactive connections
2020-05-17 10:20:09.938 DEBUG 32600 --- [ctor-http-nio-1] r.netty.http.client.HttpClientConnect    : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Handler is being applied: {uri=https://www.flexiquiz.com/api/v1/users/{userid}/quizzes, method=POST}
2020-05-17 10:20:09.939 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] onStateChange(POST{uri=/api/v1/users/{userid}/quizzes, connection=PooledConnection{channel=[id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443]}}, [request_prepared])
2020-05-17 10:20:09.939 DEBUG 32600 --- [ctor-http-nio-1] o.s.http.codec.json.Jackson2JsonEncoder  : [1bbedd72] Encoding [QuizRequest(quizId={quizid})]
2020-05-17 10:20:09.941 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] onStateChange(POST{uri=/api/v1/users/{userid}/quizzes, connection=PooledConnection{channel=[id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443]}}, [request_sent])
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.http.client.HttpClientOperations     : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Received response (auto-read:false) : [Cache-Control=private, Content-Type=text/html; charset=utf-8, Server=Microsoft-IIS/10.0, Date=Sun, 17 May 2020 04:50:10 GMT, Content-Length=30]
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] onStateChange(POST{uri=/api/v1/users/{userid}/quizzes, connection=PooledConnection{channel=[id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443]}}, [response_received])
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] o.s.w.r.f.client.ExchangeFunctions       : [1bbedd72] Response 400 BAD_REQUEST
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.http.client.HttpClientOperations     : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Received last HTTP packet
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] onStateChange(POST{uri=/api/v1/users/{userid}/quizzes, connection=PooledConnection{channel=[id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443]}}, [response_completed])
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] onStateChange(POST{uri=/api/v1/users/{userid}/quizzes, connection=PooledConnection{channel=[id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443]}}, [disconnecting])
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Releasing channel
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Channel cleaned, now 0 active connections and 1 inactive connections
2020-05-17 10:20:10.190 DEBUG 32600 --- [ctor-http-nio-1] reactor.netty.channel.FluxReceive        : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Subscribing inbound receiver [pending: 1, cancelled:false, inboundDone: true]
2020-05-17 10:20:10.190 DEBUG 32600 --- [ctor-http-nio-1] o.s.core.codec.StringDecoder             : [1bbedd72] Decoded "{"message":"400: Bad Request"}"
2020-05-17 10:20:10.190  INFO 32600 --- [nio-8086-exec-6] i.d.ivrauto.resource.ResponseResource    : message from status: {"message":"400: Bad Request"}
2020-05-17 10:20:10.190  INFO 32600 --- [nio-8086-exec-6] i.d.ivrauto.resource.ResponseResource    : json.get(message): 400: Bad Request
2020-05-17 10:20:10.190  INFO 32600 --- [nio-8086-exec-6] o.h.e.i.AbstractFlushingEventListener    : Processing flush-time cascades
2020-05-17 10:20:10.195 DEBUG 32600 --- [nio-8086-exec-6] o.h.e.i.AbstractFlushingEventListener    : Dirty checking collections

Below is the end point I am trying to access.

POST: /v1/users/{user_id}/quizzes

Example Request

$ curl https://www.flexiquiz.com/api/v1/users/06e3244f-1381-4da4-aa75-996981b42edb/quizzes 
-H "X-API-KEY: fcb5f59c-2a2f-44a9-8261-33cbfa97be99"
-d quiz_id="1153af46-9580-4672-af78-f23ce2577a14"

Example Response

{                             
    "message": "200: OK"
}
Altruistic answered 16/5, 2020 at 15:7 Comment(8)
can you please try sending the request without a custom user agent header.Principally
@ThomasAndolf No, it didn't help. Still get 400 Bad Request.Altruistic
Can you post your logs or run your application in debug so we can se the actual request madePrincipally
@ThomasAndolf Have edited my question with logs.Altruistic
What does your server endpoint look like? you've provided the client code but not the server method.Kunin
@RoddyoftheFrozenPeas Added sample request and response to my question.Altruistic
Your curl command is a FORM post by default, but the body() in assignQuizToCandidate serialized into application/json. They are different content type.Example
@Example but I am constructing POST request what is that I need to do now? Any idea?Altruistic
P
4

your problem is probably that you are sending the data in the wrong format. You are posting data in the body in the application/json format.

But if you look in the request it is made using the -d flag in curl.

From the curl documentation:

-d, --data

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.

Which basically means that you need to send the data in FORM format.

The Webflux documentation tells how to send the data as a form request.

Webflux Send FormData

So your code should look like something (sort of):

public QuizResponse assignQuizToCandidate(String userId, String quizId) {

    final MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
    formData.add("quiz_id", quizId);

    return webClient.post()
        .uri(FLEXIQUIZ_API_BASE_URL + "/v1/users/" + userId + "/quizzes")
        .header("X-API-KEY", FLEXIQUIZ_API_KEY)
        .bodyValue(formData)
        .retrive()
        .bodyToMono(QuizResponse.class)
        .block();
}
Principally answered 17/5, 2020 at 10:58 Comment(1)
You are absolutely correct man! I am going to accept your answer. Also, I will add my solution based on your answer.Altruistic
A
2

Based on @ThomasAndolf answer here is what I had to do.

public String assignQuizToCandidate(String userId, String quizId) {
    final MultiValueMap<String, String> data = new LinkedMultiValueMap<>();
    data.add("quiz_id", quizId);

    return webClient.post()
        .uri(FLEXIQUIZ_API_BASE_URL + "/v1/users/" + userId + "/quizzes")
        .header("X-API-KEY", FLEXIQUIZ_API_KEY)
        .bodyValue(data)
        .retrieve()
        .bodyToMono(String.class)
        .block();
  }

I took the output as a String since the response only contains a string with the following message: {"message": "200: OK"}

Altruistic answered 17/5, 2020 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.