Curl -xget works, but fail when use RESTful client in java


(Chia-Eng Chang) #1

I tried to send http/get to my elasticsearch server.if I query:

    curl 'http://localhost:9200/index/_search?scroll=1m&size=50&pretty' 

-d '{"query" : {"match_all" : {}}}'

it works perfect. But when I tried to use jersy to build my client, I did
the follwoing:

    public class JerseyClientGet {

         public static void main(String[] args) {
   
             Client client = Client.create();
             WebResource webResource = client

.resource("http://localhost:9200/index/_search?scroll=1m&size=50 -d
'{"query" : {"match_all" : {}}}'");
......
}
}

And I got these error message:

     java.lang.IllegalArgumentException: Illegal character in query at 

index 52: http://localhost:9200/obd2/_search?scroll=1m&size=50 -d '{"query"
: {"match_all" : {}}}'
at java.net.URI.create(URI.java:859)
at com.sun.jersey.api.client.Client.resource(Client.java:433)
at JerseyClientGet.main(JerseyClientGet.java:20)
Caused by: java.net.URISyntaxException: Illegal character in query
at index 52: http://localhost:9200/index/_search?scroll=1m&size=50 -d
'{"query" : {"match_all" : {}}}'
at java.net.URI$Parser.fail(URI.java:2829)
at java.net.URI$Parser.checkChars(URI.java:3002)
at java.net.URI$Parser.parseHierarchical(URI.java:3092)
at java.net.URI$Parser.parse(URI.java:3034)
at java.net.URI.(URI.java:595)
at java.net.URI.create(URI.java:857)
... 2 more

The "-d" seems to be an illegal character?
Anyone knows what's the problem with my format?
PS: I can use java API to query, just use this RESTful for some test.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/c7495d9d-bace-4c09-9f58-6b49c4888cc1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Chia-Eng Chang) #2

Updated.
I figured out that I need to do url-encode to process some characters like
{ , } ," ...
so I change part my code to:

String string1="-d {"query" : {"match_all" : {}}}";
WebResource webResource = client
.resource("http://localhost:9200/obd2/_search?scroll=1m&size=50"+
URLEncoder.encode(string1));

Now I get the respones:

            java.lang.RuntimeException: Failed : HTTP error code : 400

Is that mean my get/request was successfully sent to the server.
The new error was triggered by some other reasons such as firewall...etc

On Friday, August 1, 2014 12:25:27 PM UTC-7, Chia-Eng Chang wrote:

I tried to send http/get to my elasticsearch server.if I query:

    curl 'http://localhost:9200/index/_search?scroll=1m&size=50&pretty' 

-d '{"query" : {"match_all" : {}}}'

it works perfect. But when I tried to use jersy to build my client, I did
the follwoing:

    public class JerseyClientGet {

         public static void main(String[] args) {
   
             Client client = Client.create();
             WebResource webResource = client

.resource("http://localhost:9200/index/_search?scroll=1m&size=50 -d
'{"query" : {"match_all" : {}}}'");
......
}
}

And I got these error message:

     java.lang.IllegalArgumentException: Illegal character in query at 

index 52: http://localhost:9200/obd2/_search?scroll=1m&size=50 -d
'{"query" : {"match_all" : {}}}'
at java.net.URI.create(URI.java:859)
at com.sun.jersey.api.client.Client.resource(Client.java:433)
at JerseyClientGet.main(JerseyClientGet.java:20)
Caused by: java.net.URISyntaxException: Illegal character in
query at index 52: http://localhost:9200/index/_search?scroll=1m&size=50
-d '{"query" : {"match_all" : {}}}'
at java.net.URI$Parser.fail(URI.java:2829)
at java.net.URI$Parser.checkChars(URI.java:3002)
at java.net.URI$Parser.parseHierarchical(URI.java:3092)
at java.net.URI$Parser.parse(URI.java:3034)
at java.net.URI.(URI.java:595)
at java.net.URI.create(URI.java:857)
... 2 more

The "-d" seems to be an illegal character?
Anyone knows what's the problem with my format?
PS: I can use java API to query, just use this RESTful for some test.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/06200e63-aeff-4bf8-87ef-ce20ee7f0086%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Brian Yoder) #3

Well, the curl command uses the -d option to specify the content data to
pass to the server. It is not part of the URL. Your mistake is trying to
take a curl command-line and treat the entire thing as a URL, when in fact
the URL is only part of the request.

I don't know how the JerseyGetClient works, but here is one example I found
that might help, or at least provide a starting point.

The idea is that a typical HTTP request in Java accepts the URL at one
string, or perhaps even two strings (the server/port, and then the URI
path), and then the content type and data as separately specified values
elsewhere in the API.

Brian

On Friday, August 1, 2014 4:59:10 PM UTC-4, Chia-Eng Chang wrote:

Updated.
I figured out that I need to do url-encode to process some characters like
{ , } ," ...
so I change part my code to:

String string1="-d {"query" : {"match_all" : {}}}";
WebResource webResource = client
.resource("http://localhost:9200/obd2/_search?scroll=1m&size=50"+
URLEncoder.encode(string1));

Now I get the respones:

            java.lang.RuntimeException: Failed : HTTP error code : 400

Is that mean my get/request was successfully sent to the server.
The new error was triggered by some other reasons such as firewall...etc

On Friday, August 1, 2014 12:25:27 PM UTC-7, Chia-Eng Chang wrote:

I tried to send http/get to my elasticsearch server.if I query:

    curl '

http://localhost:9200/index/_search?scroll=1m&size=50&pretty' -d
'{"query" : {"match_all" : {}}}'

it works perfect. But when I tried to use jersy to build my client, I did
the follwoing:

    public class JerseyClientGet {

         public static void main(String[] args) {
   
             Client client = Client.create();
             WebResource webResource = client

.resource("http://localhost:9200/index/_search?scroll=1m&size=50 -d
'{"query" : {"match_all" : {}}}'");
......
}
}

And I got these error message:

     java.lang.IllegalArgumentException: Illegal character in query 

at index 52: http://localhost:9200/obd2/_search?scroll=1m&size=50 -d
'{"query" : {"match_all" : {}}}'
at java.net.URI.create(URI.java:859)
at com.sun.jersey.api.client.Client.resource(Client.java:433)
at JerseyClientGet.main(JerseyClientGet.java:20)
Caused by: java.net.URISyntaxException: Illegal character in
query at index 52: http://localhost:9200/index/_search?scroll=1m&size=50
-d '{"query" : {"match_all" : {}}}'
at java.net.URI$Parser.fail(URI.java:2829)
at java.net.URI$Parser.checkChars(URI.java:3002)
at java.net.URI$Parser.parseHierarchical(URI.java:3092)
at java.net.URI$Parser.parse(URI.java:3034)
at java.net.URI.(URI.java:595)
at java.net.URI.create(URI.java:857)
... 2 more

The "-d" seems to be an illegal character?
Anyone knows what's the problem with my format?
PS: I can use java API to query, just use this RESTful for some test.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/a76d932e-4f64-48ca-9306-16704aeef156%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Chia-Eng Chang) #4

Got the answer!

It turns out that just add an "&" between the url and encoded json body.

String string1="{"query" : {"match_all" : {}}}";
WebResource webResource = client
.resource("http://localhost:9200/obd2/_search?scroll=1m&size=50&"+
URLEncoder.encode(string1));

then the http/get was successfully transferred and return the query result.

On Friday, August 1, 2014 2:10:26 PM UTC-7, Brian wrote:

Well, the curl command uses the -d option to specify the content data to
pass to the server. It is not part of the URL. Your mistake is trying to
take a curl command-line and treat the entire thing as a URL, when in fact
the URL is only part of the request.

I don't know how the JerseyGetClient works, but here is one example I
found that might help, or at least provide a starting point.

http://crunchify.com/create-very-simple-jersey-rest-service-and-send-json-data-from-java-client/

The idea is that a typical HTTP request in Java accepts the URL at one
string, or perhaps even two strings (the server/port, and then the URI
path), and then the content type and data as separately specified values
elsewhere in the API.

Brian

On Friday, August 1, 2014 4:59:10 PM UTC-4, Chia-Eng Chang wrote:

Updated.
I figured out that I need to do url-encode to process some characters
like { , } ," ...
so I change part my code to:

String string1="-d {"query" : {"match_all" : {}}}";
WebResource webResource = client
.resource("http://localhost:9200/obd2/_search?scroll=1m&size=50"+
URLEncoder.encode(string1));

Now I get the respones:

            java.lang.RuntimeException: Failed : HTTP error code : 400

Is that mean my get/request was successfully sent to the server.
The new error was triggered by some other reasons such as firewall...etc

On Friday, August 1, 2014 12:25:27 PM UTC-7, Chia-Eng Chang wrote:

I tried to send http/get to my elasticsearch server.if I query:

    curl '

http://localhost:9200/index/_search?scroll=1m&size=50&pretty' -d
'{"query" : {"match_all" : {}}}'

it works perfect. But when I tried to use jersy to build my client, I
did the follwoing:

    public class JerseyClientGet {

         public static void main(String[] args) {
   
             Client client = Client.create();
             WebResource webResource = client

.resource("http://localhost:9200/index/_search?scroll=1m&size=50 -d
'{"query" : {"match_all" : {}}}'");
......
}
}

And I got these error message:

     java.lang.IllegalArgumentException: Illegal character in query 

at index 52: http://localhost:9200/obd2/_search?scroll=1m&size=50 -d
'{"query" : {"match_all" : {}}}'
at java.net.URI.create(URI.java:859)
at com.sun.jersey.api.client.Client.resource(Client.java:433)
at JerseyClientGet.main(JerseyClientGet.java:20)
Caused by: java.net.URISyntaxException: Illegal character in
query at index 52: http://localhost:9200/index/_search?scroll=1m&size=50
-d '{"query" : {"match_all" : {}}}'
at java.net.URI$Parser.fail(URI.java:2829)
at java.net.URI$Parser.checkChars(URI.java:3002)
at java.net.URI$Parser.parseHierarchical(URI.java:3092)
at java.net.URI$Parser.parse(URI.java:3034)
at java.net.URI.(URI.java:595)
at java.net.URI.create(URI.java:857)
... 2 more

The "-d" seems to be an illegal character?
Anyone knows what's the problem with my format?
PS: I can use java API to query, just use this RESTful for some test.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/c5f9d940-4301-4423-9861-3dbb6da781cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #5