Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public void creatUser()
{
String url = "http://localhost:" + port + "/users";
String username = "chathuranga";
String password = "123";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
AddUserRequest addUserRequest = new AddUserRequest();
addUserRequest.setName("Sample User");
addUserRequest.setUsername("user1");
addUserRequest.setPassword("pass123");
HttpEntity<AddUserRequest> requestEntity = new HttpEntity<>(addUserRequest, requestHeaders);
//basic authentication is made with 'withBasicAuth' method available in the TestRestTemplate
ResponseEntity<AddUserResponse> responseEntity = testRestTemplate.withBasicAuth(username, password)
.exchange(
url,
HttpMethod.POST,
requestEntity,
AddUserResponse.class
);
if (responseEntity.getStatusCode() == HttpStatus.OK)
{
AddUserResponse addUserResponse = responseEntity.getBody();
System.out.println(addUserResponse);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.