javaapiuribuilder

Java URI Builder build method replacing "?" character in the path


I have an API at the following path

/v0/segments/ch/abc/view/status/ACTIVE?sc=%s&expiryGteInMs=%d

I am building a Client using the URIBuilder in Java.

return UriBuilder
      .fromUri(config.getHost())
      .path(String.format(config.getPath(),request.List(), request.getTime()))
      .build();

The request contains a list to be substituted in place of %s and the time to be substituted in place of %d. But the request being formed has a path like this

/v0/segments/ch/abc/view/status/ACTIVE%3Fsc=FK,GR&expiryGteInMs=1611081000000

Basically the "?" character is being replaced by %3F. Can somebody help me with this, please?

P.S: I know that we can use the ".queryParam" option available in URIBuilder. Looking for the actual reason why this is happening?


Solution

  • Most probably library that you are using is encoding url, and ? encodes to %3F. Why this happens(in short): url could contain only specific set of character, and ? is not one of them, so, in order to transfer this character, we should encode it (so called Percent-encoding).

    A bit longer explanation (taken from here):

    URL encoding converts characters into a format that can be transmitted over the Internet.

    URLs can only be sent over the Internet using the ASCII character-set.

    Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.

    URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.

    URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.