2

I am trying to build a URI having a trailing slash but can't happen to find the way to do it with URIBuilder. Is that even possible?

Expected result would be: http://test.com:24/?param1=value1&param2=value2

@Test
public void ttt(){
    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setScheme("http");
    uriBuilder.setHost("test.com");
    uriBuilder.setPort(24);
    uriBuilder.addParameter("param1", "value1");
    uriBuilder.addParameter("param2", "value2");
    System.out.println(uriBuilder.toString());
}

What I get at the moment: http://test.com:24?param1=value1&param2=value2

Any idea?

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Xbreizh
  • 333
  • 4
  • 19
  • what purpose is the slash supposed to have? There should be no difference on the server handling both requests. – luk2302 Mar 02 '21 at 13:15
  • I am trying to rebuild an existing function and, comparing the results, that's the only difference I can spot. I imagine that some other services might filter that syntax so I m trying to reproduce – Xbreizh Mar 02 '21 at 13:20
  • It would be useful to say which URIBuilder you are using. With Google I only found two versions of UriBuilder (different letter case) and URI.builder() but not URIBuilder. I only learnt which one it was by looking at an answer. – k314159 Mar 03 '21 at 09:44
  • I mentioned it in the title: "apache URIBuilder" but that's a good point, I should have linked the documentation to avoid confusion [link](https://hc.apache.org/httpcomponents-core-5.0.x/current/httpcore5/apidocs/org/apache/hc/core5/net/URIBuilder.html) – Xbreizh Mar 03 '21 at 09:46

2 Answers2

2

There is setPath(str) method, see URIBuilder.setPath(String)

So your code should look like

@Test
public void ttt(){
    final URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setScheme("http");
    uriBuilder.setHost("test.com");
    uriBuilder.setPort(24);
    uriBuilder.setPath("/");
    uriBuilder.addParameter("param1", "value1");
    uriBuilder.addParameter("param2", "value2");
    System.out.println(uriBuilder.toString());
}

Output

http://test.com:24/?param1=value1&param2=value2
rkosegi
  • 14,165
  • 5
  • 50
  • 83
0

You can always add manually the slash:

int len = "http://test.com:24".length()
String newstr = uriBuilder.toString().substring(0,len) + "/" + uriBuilder.toString().substring(len);
System.out.println(newstr)

Since you know the url (you have built it manually) you can use this way to add the slash.
More general solution is to find the second ':' and add slash after the last digit of the port:

String str = uriBuilder.toString();
int ind = str.indexOf(':',6) + 1;
while(ind < str.length() && Character.isDigit(str.charAt(ind)))
    ind++;
String newstr = str.substring(0,ind) + "/" + str.substring(ind);
Raz K
  • 167
  • 8
  • I think that converting the URIBuilder into String would defeat the purpose of trying to secure the URI. Also, I simplified the code here but the scheme, port, host and parameters are all provided variables. – Xbreizh Mar 02 '21 at 14:12