0

I am consuming an API and this one in particular response with nothing but status code 201 in postman. In my code, how do I extract the status code using WebClient?

  public HttpStatus createUser(String uuid) throws Exception {

        MultiValueMap<String, String> bodyValues = new LinkedMultiValueMap<>();
        bodyValues.add("providerCallbackHost", "https://webhook.site/c6c0a388-2af5-41e1-8d6d-c280820affad");

        try {
            HttpStatus response = webClient.post()
                    .uri("https://sandbox.momodeveloper.mtn.com/v1_0/apiuser")
                    .header("X-Reference-Id", uuid)
                    .header("Ocp-Apim-Subscription-Key", SUBSCRIPTION_KEY_SECONDARY)
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(BodyInserters.fromValue(bodyValues))
                    .retrieve()
                    .bodyToMono(HttpStatus.class)
                    .block();

            return response;

        } catch (Exception exception) {
            throw new Exception(exception.getMessage());
        }
    }
markspace
  • 10,621
  • 3
  • 25
  • 39
lungu
  • 47
  • 2
  • 10
  • 2
    Does this answer your question? [How to extract response header & status code from Spring 5 WebClient ClientResponse](https://stackoverflow.com/questions/50223891/how-to-extract-response-header-status-code-from-spring-5-webclient-clientrespo) – Chin Huang Jan 26 '22 at 19:09
  • I saw this. None of them worked. – lungu Jan 26 '22 at 20:05
  • What did you try and what errors did you get? – João Dias Jan 26 '22 at 20:07

0 Answers0