Make a Post request

Report a typo

In this task, you will use RestTemplate to make a POST request to a RESTful API. Here's what you need to do:

  1. Create a RestTemplate object in your code.

  2. Create a User object.

  3. Create an HttpHeaders object and set the content type.

  4. Use the postForEntity method to send a POST request to the API endpoint.

  5. Convert response body into the User object

  6. Log the user.

Reorder lines using drag or arrows. Adjust indentation with left buttons
                HttpHeaders headers = new HttpHeaders();
              
                User newUser = new User("newuser", "password");
              
                System.out.println("User created with username: " + createdUser.getUsername());
              
                RestTemplate restTemplate = new RestTemplate();
              
                ResponseEntity<User> response = restTemplate.postForEntity(apiUrl, request, User.class);
              
                headers.setContentType(MediaType.APPLICATION_JSON);
              
                HttpEntity<User> request = new HttpEntity<>(newUser, headers);
              
                User createdUser = response.getBody();
              
___

Create a free account to access the full topic