Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.59k stars 593 forks source link

Reserved urlencoded path symbols get java.net.URISyntaxException: Illegal character in path #391

Closed konst123 closed 3 years ago

konst123 commented 3 years ago

I need to request a url with square brackets in the path, like host://www.domain.com/[something]/etc. I do URLEncoder.encode the path before passing the url to Unirest.get(url). Anyway, this generate the exception: java.net.URISyntaxException: Illegal character in path at index... in SystemDefaultRoutePlanner, line targetURI = new URI(target.toURI()); I think the problem is that kong.unirest.apache.BaseApacheClient determineTarget uses URIUtils.extractHost(requestURI) that accept encoded URI but lose this encoding and return HttpHost without encoding. Perhaps org.apache.http.client.utils.URIUtils should not be used in kong.unirest.apache.BaseApacheClient

ryber commented 3 years ago

You can't have square brackets in a directory path, it's against the URI spec, so encoding them is the only way to get it is to encode it. I'm not sure how you are doing that but here are two examples that work:

         Unirest.get("host://www.domain.com/" + URLEncoder.encode("[brackets]") + "/etc")
                .asSting();

        Unirest.get("host://www.domain.com/{param}/etc.")
                .routeParam("param", "[something]")
                .asString();

I added a test showing this working:

https://github.com/Kong/unirest-java/commit/c5c714a545b63c36f5cf914c881d3722104878e9

konst123 commented 3 years ago

Thank you very much. Yes, it works OK. It was my fault - I encoded the whole path (with the first slash).