Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.URISyntaxException: Illegal character in path at index 75

I'm trying to send GET request from java through Apache REST client and encountered this issue.

java.net.URISyntaxException: Illegal character in path at index 75: http://torrento.sharepoint.com/_api/web/getfolderbyserverrelativeurl('/Shared Documents/test')/files at java.net.URI$Parser.fail(URI.java:2848) at java.net.URI$Parser.checkChars(URI.java:3021) at java.net.URI$Parser.parseHierarchical(URI.java:3105) at java.net.URI$Parser.parse(URI.java:3053) at java.net.URI.(URI.java:588) at org.apache.http.client.utils.URIBuilder.(URIBuilder.java:82) at com.mstack.samples.sharepoint.SharepointApp.getAllFiles(SharepointApp.java:61) at com.mstack.samples.sharepoint.SharepointApp.main(SharepointApp.java:45)

Code snippet :-

            httpClient = HttpClientBuilder.create().build();
            uriBuilder = new URIBuilder(requestUrl);
            System.out.println(uriBuilder);
            httpGet = new HttpGet(uriBuilder.build());
            httpGet.addHeader(AUTHORIZATION, "Bearer " + TOKEN);
            httpGet.addHeader("accept", "application/json; odata=verbose");
            response = httpClient.execute(httpGet);

Where requestUrl is http://torrento.sharepoint.com/_api/web/getfolderbyserverrelativeurl('/Shared Documents/test')/files

I know the space between Shared and Documents is the issue. Tried to encode it. But that too didn't work. Please help

like image 612
Sachin Avatar asked Jun 30 '16 12:06

Sachin


People also ask

How do you fix URISyntaxException?

net. URISyntaxException: Illegal character in path at index. To fix this issue remove the whitespace in the file.

What characters are illegal path?

The "Illegal characters" exception means that the file path string you are passing to ReadXml is wrong: it is not a valid path. It may contain '?' , or ':' in the wrong place, or '*' for example. You need to look at the value, check what it is, and work out where the illegal character(s) are coming from.

What is Java net URISyntaxException?

URISyntaxException . This is a checked exception that occurs when you are trying to parse a string that represents a URI, but it doesn't have the correct format.


1 Answers

I've obtained the solution by simply adding requestUrl.replaceAll(" ", "%20"); But in case of other special characters this alone won't work. So we must encode url before sending request.

Cheers :)

like image 194
Sachin Avatar answered Oct 12 '22 21:10

Sachin