Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP get with headers using RestTemplate

How can I send a GET request using the Spring RestTemplate? Other questions have used POST, but I need to use GET. When I run this, the program continues to work, but it seems that the network is clogged because this is in an AsyncTask, and when I try to run another asynctask after I click on the button for this one, they won't work.

I tried doing

String url = "https://api.blah.com/2.0/search/cubes?w=jdfkl&whitespace=1";  MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.add("Bearer", accessToken);  HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //copied this from somewhere else, not sure what its for  HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);  HttpMessageConverter<String> stringConverter = new StringHttpMessageConverter(); FormHttpMessageConverter formConverter = new FormHttpMessageConverter(); List<HttpMessageConverter<?>> msgConverters = new ArrayList<HttpMessageConverter<?>>();   msgConverters.add(formConverter); msgConverters.add(new MappingJacksonHttpMessageConverter()); msgConverters.add(stringConverter);   template.setMessageConverters(msgConverters); //SetSearchResponseData is my custom class to store the incoming JSON ResponseEntity<SetSearchResponseData> result = template.exchange(url, HttpMethod.GET, request, SetSearchResponseData.class); //If I was using post, i could have done SetSearchResponseDataresponse = restTemplate.postForObject(url, request, SetSearchResponseData.class); 
like image 318
rasen58 Avatar asked May 27 '13 23:05

rasen58


People also ask

How do I set HTTP headers in RestTemplate?

To answer this question, you can use one of the exchange methods that accepts an HttpEntity for which you can also set the HttpHeaders. (You can also sepecify the HTTP method you want to use). RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders();

How do you pass the headers on a RestTemplate call?

Instead of setting headers by using dedicated methods (like setAccept in previous code), you can use general set (headerName, headerValue) method. headers. set("Accept", "application/json");

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.


2 Answers

The RestTemplate getForObject() method does not support setting headers. The solution is to use the exchange() method.

So instead of restTemplate.getForObject(url, String.class, param) (which has no headers), use

HttpHeaders headers = new HttpHeaders(); headers.set("Header", "value"); headers.set("Other-Header", "othervalue"); ...  HttpEntity<Void> requestEntity = new HttpEntity<>(headers);  ResponseEntity<String> response = restTemplate.exchange(     url, HttpMethod.GET, requestEntity, String.class, param); 

Finally, use response.getBody() to get your result.

This question is similar to this question.

like image 96
Richard Neish Avatar answered Sep 25 '22 17:09

Richard Neish


Take a look at the JavaDoc for RestTemplate.

There is the corresponding getForObject methods that are the HTTP GET equivalents of postForObject, but they doesn't appear to fulfil your requirements of "GET with headers", as there is no way to specify headers on any of the calls.

Looking at the JavaDoc, no method that is HTTP GET specific allows you to also provide header information. There are alternatives though, one of which you have found and are using. The exchange methods allow you to provide an HttpEntity object representing the details of the request (including headers). The execute methods allow you to specify a RequestCallback from which you can add the headers upon its invocation.

like image 23
nicholas.hauschild Avatar answered Sep 24 '22 17:09

nicholas.hauschild