Created
May 13, 2018 10:49
-
-
Save chathurangat/99b954d93500772a54144516fd48e5c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void findUserById() | |
{ | |
String username = "chathuranga"; | |
String password = "123"; | |
Integer userId = 1; | |
String url = "http://localhost:8080/users/" + userId; | |
//setting up the HTTP Basic Authentication header value | |
String authorizationHeader = "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes()); | |
HttpHeaders requestHeaders = new HttpHeaders(); | |
//set up HTTP Basic Authentication Header | |
requestHeaders.add("Authorization", authorizationHeader); | |
requestHeaders.add("Accept", MediaType.APPLICATION_JSON_VALUE); | |
//request entity is created with request headers | |
HttpEntity<AddUserRequest> requestEntity = new HttpEntity<>(requestHeaders); | |
ResponseEntity<FindUserResponse> responseEntity = restTemplate.exchange( | |
url, | |
HttpMethod.GET, | |
requestEntity, | |
FindUserResponse.class | |
); | |
if (responseEntity.getStatusCode() == HttpStatus.OK) { | |
System.out.println("response received"); | |
System.out.println(responseEntity.getBody()); | |
} else { | |
System.out.println("error occurred"); | |
System.out.println(responseEntity.getStatusCode()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment