Reputation: 331
The following call in curl is working and the server response is a 200 code:
curl -i -H "Content-Type: application/json" -H "Cookie: wv_sess=emrri58a7f3qauof5ntfsc77k7" -X GET http://192.168.1.1/cli/aos?cmd=show+interfaces+1/1/1
But when i try doing the same with RestTemplate in Spring with the following code:
String cookies = "wv_sess=emrri58a7f3qauof5ntfsc77k7";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Cookie",cookies);
headers.set("Content-Type",MediaType.APPLICATION_JSON_VALUE);
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange("http://192.168.1.1/cli/aos?cmd=show+interfaces+1/1/1", HttpMethod.GET, entity, String.class);
logger.info(response.toString());
i get a 400 code so it makes HttpClient crash:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 400 Bad Request] with root cause
What is the equivalent from the curl sentence to RestTemplate? Does someone have any suggestion to fix it?
NOTE: I making requests to WebServices from an Alcatel 6860 switch.
Upvotes: 2
Views: 4196
Reputation: 331
I found the solution, thanks anyway for the replies.
The problem was that RestTemplate was encoding my URL, i detected it via Wireshark.
Given the original URL:
http://192.168.1.1/cli/aos?cmd=show+interfaces+1/1/1
it got replaced the "+" symbol and the following was the result:
http://192.168.1.1/cli/aos?cmd=show%2Binterfaces%2B1/1/1
So the server of course detected an invalid URL and have to answer with a 400 code error.
To fix it i replaced the symbol "+" for a space which encoded is a "+".
Upvotes: 2
Reputation: 965
From your error:
nested exception is
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
it looks like your request is going through, and the server is responding with a 400 Bad Request. Spring RestTemplate throws an HttpClientErrorException
whenever the server returns a 4xx error code.
Upvotes: 0