Skip to content

Instantly share code, notes, and snippets.

@chathurangat
Created May 17, 2018 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chathurangat/5528b72243820fcc1d587f4635a16350 to your computer and use it in GitHub Desktop.
Save chathurangat/5528b72243820fcc1d587f4635a16350 to your computer and use it in GitHub Desktop.
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