2

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.

Jordi
  • 331
  • 2
  • 15

2 Answers2

2

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 "+".

Jordi
  • 331
  • 2
  • 15
  • Thank you. This fixed it for me. I was sending requests to Salesforce Rest API and it was failing in RestTemplate and successful in CURL. I just had to replace the '+'s with spaces in my Query. – Airwavezx Jun 29 '20 at 15:03
0

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.

Jan
  • 848
  • 7
  • 17
  • So, what I doing different in RestTemplate that makes it not working? Maybe the problem is in the Content-Type attribute but I tried lots of combinations for it and still not working. – Jordi Jun 21 '17 at 15:17
  • Have you tried logging out the full request you're making? Do something like what is described here: https://stackoverflow.com/a/22620168/530728 . That should help you determine what the difference is between your curl request and your RestTemplate request. – Jan Jun 21 '17 at 15:57
  • Why a server returns a 400 Bad Request status depends completely on the server ... Can you enable verbose logs in the server or anything like that? – Jan Jun 21 '17 at 16:02
  • I tried with the Interceptor but i just get the objects references. – Jordi Jun 21 '17 at 16:16