I need to send a GET request with a header: Content-Type: application/camiant-msr-v2.0+xml
. I expect an XML response from the server. I tested the request and response with Postman and everything is good. But when I try to do it in Spring with RestTemplate
, I always get a 400 bad request. The exceptions from spring
are:
Jul 09, 2016 12:53:38 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [/smp] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 400 Bad Request] with root cause
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475)
My code:
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Content-Type", "application/camiant-msr-v2.0+xml");
HttpEntity<?> entity = new HttpEntity<Object>(headers);
log.debug("request headers: " + entity.getHeaders());
ResponseEntity<String> response = restTemplate.exchange(queryUrl, HttpMethod.GET, entity, String.class);
The debug message shows the header as {Content-Type=[application/camiant-msr-v2.0+xml]}
, which seems to be correct. I wonder what's wrong with my request and if there's a way to see the requests on the wire to debug.
To add custom request headers to an HTTP GET request, you should use the generic exchange() method provided by the RestTemplate class.
HttpHeaders headers = new HttpHeaders(); headers. set("Header", "value"); headers. set("Other-Header", "othervalue"); ... HttpEntity<Void> requestEntity = new HttpEntity<>(headers); ResponseEntity<String> response = restTemplate.
Using Filter we can add custom headers to all response and while using ResponseEntity or HttpServletResponse we can write headers to the particular or individual response. NOTE : All three ways not only limited to spring boot, It part of Spring web MVC so it will also work with Spring MVC application.
Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value. Multiple parameters are separated by “&” symbol.
The following worked for me:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
@Service
public class Http {
@Autowired
private RestTemplate restTemplate;
public String doGet(final String url) throws Exception {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add(HttpHeaders.USER_AGENT, "Mozilla/5.0");
headers.add(HttpHeaders.ACCEPT_LANGUAGE, "en-US,en;q=0.8");
HttpEntity<?> entity = new HttpEntity<Object>(headers);
HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
return response.getBody();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With