question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TestRestTemplate does the url encoding twice if I pass the URI as a string

See original GitHub issue

Controller:

@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:closed
  • Created 6 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
manish-pcycommented, May 24, 2018

Just using new URI(enocodedUrl) in RestTemplateCall solves the issue… UriComponentsBuilder can also be used to prepare encodedUrl from MultiValueMap. Example :

String enocodedUrl = UriComponentsBuilder.fromHttpUrl(URL).queryParams(queryParameterMultiValueMap).build(true).toUriString();

restTemplate.getForEntity(new URI(enocodedUrl ) , String.class);
8reactions
georgmittendorfercommented, Jun 11, 2018

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
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found