Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RestTemplate to POST request with Custom Headers and a Request Object

In Spring RestTemplate is there a way to send Custom Headers together with a POST Request Object. I have already tried out the exchange method which is available. It seems that we can send key value pairs together with a custom headers but not a request object itself attached to the HttpEntity. The following code illustrates the attempt and it seems to be 400 BadRequest for the server.

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<?> httpEntity = new HttpEntity<Object>(requestDTO, requestHeaders);

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.exchange(URL, HttpMethod.POST, httpEntity, SomeObject.class);

Anyone aware about this situation ? Or is it something which is not possible that Im trying to do ?

like image 988
MCF Avatar asked Feb 12 '14 09:02

MCF


People also ask

How do you set headers in Spring RestTemplate?

I suggest using one of the exchange methods that accepts an HttpEntity for which you can also set the HttpHeaders . (You can also specify the HTTP method you want to use.) For example, RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.

Which method of RestTemplate is used for sending post request?

RestTemplate's postForObject method creates a new resource by posting an object to the given URI template. It returns the result as automatically converted to the type specified in the responseType parameter.

How do you use RestTemplate in Spring boot Post?

Consuming POST API by using RestTemplate - exchange() method Assume this URL http://localhost:8080/products returns the response shown below, we are going to consume this API response by using the Rest Template. Autowired the Rest Template Object. Use the HttpHeaders to set the Request Headers.

What is difference between getForObject and getForEntity?

For example, the method getForObject() will perform a GET and return an object. getForEntity() : executes a GET request and returns an object of ResponseEntity class that contains both the status code and the resource as an object. getForObject() : similar to getForEntity() , but returns the resource directly.


1 Answers

Yes, It is possible, if use MultiValueMap headers instead of HttpHeaders

Example:

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Authorization", "Basic " + base64Creds);
headers.add("Content-Type", "application/json");

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

HttpEntity<ObjectToPass> request = new HttpEntity<ObjectToPass>(objectToPassInstance, headers);

restTemplate.postForObject(urlPost, request, Boolean.class);

Boolean.class just because my controller returns boolean at this endpoint (could be anything)

Good luck with coding!

like image 67
Andrew Avatar answered Oct 16 '22 14:10

Andrew