Senthil
Senthil

Reputation: 131

org.springframework.web.client.HttpClientErrorException: 401 null

I am calling a service from one module to another module. but it throws an exception:

org.springframework.web.client.HttpClientErrorException: 401 null

POST request for "http://localhost:80../.../...Report" resulted in 401 (null); invoking error handler.

I can able to hit this service from post man directly. But when its called from spring boot module it throws error.

BaseResponseDTO baseResponseDTO = restTemplate.postForObject("http://localhost:80../.../...Report", requestDTO, BaseResponseDTO.class);

error throws in this line

Upvotes: 4

Views: 8087

Answers (1)

Senthil
Senthil

Reputation: 131

I added authorization headers in MultiValueMap and i added object request in HttpEntity. here i added code.

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Authorization", "bearer "+<TOKEN>);
HttpEntity<MyObject> httpEntityRequest = new HttpEntity<>(myObjectReference, headers);

BaseResponseDTO baseResponseDTO = restTemplate.postForObject("http://localhost:80../.../...Report", httpEntityRequest , BaseResponseDTO.class);

Upvotes: 2

Related Questions