1

I have following code:

    String convertedBuilder = builder.toUriString();
    convertedBuilder = convertedBuilder.replace(",", "\\,");



    URI uri = UriComponentsBuilder.fromUriString(convertedBuilder)
              .build().toUri();

The idea is to replace a single comma ',' by a '\,' (slash and comma). Originated URL should be something like

'server-url'?name=te%5C%2Cst for parameter value 'te\,st.

However Spring generates this one:

'server-url'?name=te%5C%252Cst

What am I doing wrong?

Regards,

LuCio
  • 5,055
  • 2
  • 18
  • 34
Luís Palma
  • 249
  • 1
  • 7
  • 15
  • Why do you want to encode the `,`? With `.build(false)` you can tell the `UriComponentsBuilder` that it's not encoded. It will encode `\` to `%5C`. – Sotirios Delimanolis Sep 13 '18 at 19:35
  • @SotiriosDelimanolis, it's an URL exposed by a Salesforce endpoint and I have to send to them as '%5C%2C' – Luís Palma Sep 13 '18 at 19:46
  • What is value of `convertedBuilder` before the replace? Please provide [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), because I don't see `,` being encoded as anything, using code in question. – Andreas Sep 13 '18 at 19:54
  • @Andreas **https://xxxxxx/services/apexrest/EnterpriseObjects/serviceAccount?name=te,st** is the convertedBuilder string. – Luís Palma Sep 13 '18 at 20:02
  • *Unable to reproduce:* When I run that code, using the value you just gave, I get `https://xxxxxx/services/apexrest/EnterpriseObjects/serviceAccount?name=te%5C,st`, i.e. no encoding of `,`, when using `spring-web-4.3.10.RELEASE.jar`. – Andreas Sep 13 '18 at 20:09
  • 1
    Yes correct! @Andreas I got what you've got, I would like now how to have **https://xxxxxx/services/apexrest/EnterpriseObjects/serviceAccount?name=te%5C%2Cst** instead of **https://xxxxxx/services/apexrest/EnterpriseObjects/serviceAccount?name=te%5C,st** – Luís Palma Sep 13 '18 at 20:31
  • `URI uri = URI.create(convertedBuilder.replace(",", "%5C%2C"));` – Andreas Sep 13 '18 at 20:57

2 Answers2

1

I had a similar requirement where I had to call a service with a GPS coordinate like this:

http://example.com/path?param1=40.678806%2C-73.943244

UriComponentsBuilder alone would either:

  • not encode the , (comma) within the parameter value
  • encode the encoded comma if I already had encoded the parameter with URLEncoder, thus producing the value 40.678806%252C-73.943244

I resolved by explicitly encoding the query parameter with URLEncoder and telling UriComponentsBuilder that the URL was already encoded:

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://example.com/path")
        .queryParam("param1" , URLEncoder.encode("40.678806,-73.943244", "UTF-8"));

ResponseEntity<String> resp = template.exchange(
        builder.build(true).toUri(),
        HttpMethod.GET, 
        null, new ParameterizedTypeReference<String> () {}
    );
watery
  • 5,026
  • 9
  • 52
  • 92
0

How to encode comma in Spring UriComponentsBuilder?

UriComponentsBuilder doesn't encode the comma since RFC doesn't require it to be encoded. You will have to encode it manually as suggested by Andreas in the comments.

However Spring generates this one: 'server-url'?name=te%5C%252Cst

No, it doesn't, as also mentioned by Andreas. It encodes the backslash but not the comma. Please fix the question if you need any more information.

rougou
  • 956
  • 9
  • 14