0

I am trying to make a request as below

http://www.google.com/query?type=dvSwitch&format=records&fields=moref,name,vc,vcName&pageSize=10&filter=((isVMware==true,moref==dvs-59);isVCEnabled==true)

When i make this request on browser, browser doesn't encode any character in the url.

enter image description here

But when i try to make the same request with RestTemplate, it encodes = character to %3D.

RestTemplate restTemplate = new RestTemplate();
        restTemplate.exchange("http://www.google.com/query?type=dvSwitch&format=records&fields=moref,name,vc,vcName&pageSize=10&filter=((isVMware==true,moref==dvs-59);isVCEnabled==true)", HttpMethod.GET,null,String.class );

It converts the url to below format:

http://www.google.com/query?type=dvSwitch&format=records&fields=moref,name,vc,vcName&pageSize=10&filter=((isVMware%3D%3Dtrue,moref%3D%3Ddvs-59);isVCEnabled%3D%3Dtrue)

I want to make request without query parameters being encoded like that because this prevents endpoint working right.

How can make RestTemplate stop encoding query parameters?

Community
  • 1
  • 1
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69
  • When you make your request with RestTemplate, are you just passing in the whole url, or are you actually using query parameters? It would be nice to see how you are making the call in your actual code... – hooknc Feb 05 '18 at 16:35
  • 1
    You can't / shouldn't, because equals characters (`=`) are supposed to be escaped as `%3D`, and the server is required to unescape them. A URL with `=` in a query parameter value is invalid. `RestTemplate` is doing the right thing, and you're not when you paste into the browser. If the server cannot handle the escaping, then the server is buggy and should be fixed. – Andreas Feb 05 '18 at 16:35
  • @hooknc I am using the code in the question. I am giving the whole url. – Salih Erikci Feb 05 '18 at 16:36
  • @Andreas Actually the endpoint is VCloud API. And it returns an error when i go with the encoded url. – Salih Erikci Feb 05 '18 at 16:37
  • @Andreas if it is wrong why browser doesn't need to encode it? It encodes some characters like ü,ş,ö,ç but not the = character. – Salih Erikci Feb 05 '18 at 16:41
  • Try to make the call using UriComponentsBuilder: https://stackoverflow.com/questions/28182836/resttemplate-to-not-escape-url – Jaime S Feb 05 '18 at 16:45
  • Riffing off of Jaime S' comment: https://stackoverflow.com/a/25434451/42962 – hooknc Feb 05 '18 at 16:52
  • The URL standard specifies that *all* characters not specific to the URL structure can be %-encoded. If a server doesn't handle that, the server is flawed. E.g. the URL for this page is https://stackoverflow.com/questions/48627111/resttemplate-url-encoding, but can be specified as https://%73%74%61%63%6B%6F%76%65%72%66%6C%6F%77%2E%63%6F%6D/%71%75%65%73%74%69%6F%6E%73/%34%38%36%32%37%31%31%31/%72%65%73%74%74%65%6D%70%6C%61%74%65%2D%75%72%6C%2D%65%6E%63%6F%64%69%6E%67. Try it in a browser. It works. – Andreas Feb 05 '18 at 16:57
  • Possible duplicate of [RestTemplate to NOT escape url](https://stackoverflow.com/questions/28182836/resttemplate-to-not-escape-url) – Turtle Feb 05 '18 at 17:00

0 Answers0