TestRestTemplate does the url encoding twice if I pass the URI as a string
See original GitHub issueController:
@RestController
@RequestMapping(value = "/uritest")
public class TestController {
@RequestMapping(method = { RequestMethod.GET })
public ResponseEntity<Void> endpoint(@RequestParam(required = true) String param) {
System.out.println(param);
return new ResponseEntity<>(HttpStatus.OK);
}
}
Integration Test:
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class ControllerTest {
@Autowired
private TestRestTemplate testRestTemplate;
@Rule
public WireMockRule wireMockRule = new WireMockRule();
HttpHeaders headers;
@Test
public void testGivenExistingStateRequestWhenCallGetStateThenReturnOkStatusAndProperBody() throws Exception {
final UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromPath("/uritest");
uriBuilder.queryParam("param", "\"\\%");
final HttpEntity<String> entity = new HttpEntity<>(null, headers);
final URI uri = uriBuilder.build().toUri();
testRestTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
testRestTemplate.exchange(uri.toString(), HttpMethod.GET, entity, String.class);
}
}
Output:
"\%
%22%5C%25
There is already a closed issue about this bug: https://github.com/spring-projects/spring-boot/issues/8163
I checked the fix there and it adds a root to the URI if the URI was relative. This issue also happens if the provided string describes a relative URI. I assume the same check should be applied to all of the other methods too where the first parameter is a String instead of an URI.
Issue Analytics
- State:
- Created 6 years ago
- Comments:13 (4 by maintainers)
Top Results From Across the Web
How to avoid double-encoding of [] when using Spring ...
If you wish to pass the encoded url directly to RestTemplate , you can instruct RestTemplate to not encode the url by passing...
Read more >Spring Boot REST Template URI Encoding - DZone
Use Spring's Rest Template to consume encoded endpoints compared to hard coded endpoints. Hard coded endpoints are prone to ...
Read more >2. RestTemplate Module - Spring
The HTTP specification allows for additional values in the Accept-Encoding header field, however RestTemplate only supports gzip compression at this time. 2.2.3 ...
Read more >Resttemplate Url Encoding - ADocLib
This function is convenient when encoding a string to be used in a query part of a ... TestRestTemplate does the url encoding...
Read more >encodeURIComponent() - JavaScript - MDN Web Docs
A string to be encoded as a URI component (a path, query string, ... not required for percent-encoding per RFC5987, // so we...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Just using
new URI(enocodedUrl)inRestTemplateCallsolves the issue…UriComponentsBuildercan also be used to prepareencodedUrlfromMultiValueMap. Example :Same issue in spring-boot 2.0.2 using UriComponentsBuilder with TestRestTemplate.
uriBuilder.toUriString()encodes space as%2520(it does:build(false).encode().toUriString()).uriBuilder.build(false).toUriString()encodes space as%20