#HttpRequest passing variable to URI create error

21 messages · Page 1 of 1 (latest)

bright dagger
#
        .uri(URI.create(ipp))
        .method("GET", HttpRequest.BodyPublishers.noBody())
        .build();
HttpResponse<String> response2 = HttpClient.newHttpClient().send(request2, HttpResponse.BodyHandlers.ofString());
System.out.println(response2.body());```


compiled but throwing this error 
```Caused by: java.net.URISyntaxException: Illegal character in authority at index 8:```
earnest oysterBOT
#

This post has been reserved for your question.

Hey @bright dagger! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

zealous token
#

what is in ipp

bright dagger
#

a variable

zealous token
#

:slow_clap:

bright dagger
#
import java.util.*;
import java.net.http.*;
import java.net.URI ;
class test {
public static void main(String[] a)
throws Throwable 
{
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("url1"))
        .method("GET", HttpRequest.BodyPublishers.noBody())
        .build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
//System.out.println(response.body());
String ipp = response.body().toString();
//System.out.println("success");
//System.out.println(ipp.toString());
//ipp = "anyurl";
HttpRequest request2 = HttpRequest.newBuilder()
        .uri(URI.create(ipp))
        .method("GET", HttpRequest.BodyPublishers.noBody())
        .build();
HttpResponse<String> response2 = HttpClient.newHttpClient().send(request2, HttpResponse.BodyHandlers.ofString());
System.out.println(response2.body());
}
}```
zealous token
bright dagger
#

well the value is not constant

#

i need to pass it as variable

zealous token
#

/run ```java
import java.net.URI ;
URI.create("u rl$1");

crude edgeBOT
#

@zealous token I only received java(15.0.2) error output

Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in path at index 1: u rl$1
	at java.base/java.net.URI.create(URI.java:906)
	at temp.main(file0.code.java:3)
Caused by: java.net.URISyntaxException: Illegal character in path at index 1: u rl$1
	at java.base/java.net.URI$Parser.fail(URI.java:2963)
	at java.base/java.net.URI$Parser.checkChars(URI.java:3134)
	at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3216)
	at java.base/java.net.URI$Parser.parse(URI.java:3175)
	at java.base/java.net.URI.<init>(URI.java:623)
	at java.base/java.net.URI.create(URI.java:904)
	at temp.main(file0.code.java:3)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.java:415)
bright dagger
#

i get the upper half http response when dry runned without second half

#

but the second http request is causing error

zealous token
#

see if i add space(illegal character) it throws an error

zealous token
bright dagger
#

it worked

#

thank you so much

earnest oysterBOT
# bright dagger thank you so much

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

bright dagger
#

String ipp = response.body().toString();
ipp = ipp.replaceAll("\s", "");