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

**URL:** <https://discuss.elastic.co/t/curl-xget-works-but-fail-when-use-restful-client-in-java/19039>\
**Category:** Elasticsearch\
**Created:** [August 1, 2014, 7:25pm UTC](https://discuss.elastic.co/t/curl-xget-works-but-fail-when-use-restful-client-in-java/19039 "2014-08-01T19:25:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chia\_Eng\_Chang](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Chia\_Eng\_Chang](https://discuss.elastic.co/u/Chia_Eng_Chang)\
**Post date:** [August 1, 2014, 7:25pm UTC](https://discuss.elastic.co/t/curl-xget-works-but-fail-when-use-restful-client-in-java/19039/1 "2014-08-01T19:25:26Z")

</div>

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](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](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](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/c7495d9d-bace-4c09-9f58-6b49c4888cc1%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Chia\_Eng\_Chang](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Chia\_Eng\_Chang](https://discuss.elastic.co/u/Chia_Eng_Chang)\
**Post date:** [August 1, 2014, 8:59pm UTC](https://discuss.elastic.co/t/curl-xget-works-but-fail-when-use-restful-client-in-java/19039/2 "2014-08-01T20:59:09Z")

</div>

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](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](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](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](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/06200e63-aeff-4bf8-87ef-ce20ee7f0086%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [August 1, 2014, 9:10pm UTC](https://discuss.elastic.co/t/curl-xget-works-but-fail-when-use-restful-client-in-java/19039/3 "2014-08-01T21:10:26Z")

</div>

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.

> **[Create Very Simple Jersey REST Service and Send JSON Data From Java Client •...](https://crunchify.com/create-very-simple-jersey-rest-service-and-send-json-data-from-java-client/)**
>
> Recently I have to pass JSON data to REST Service and did not have any simple Client handy. But created very simple Java program which read JSON data from

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](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](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](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](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](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/a76d932e-4f64-48ca-9306-16704aeef156%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Chia\_Eng\_Chang](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Chia\_Eng\_Chang](https://discuss.elastic.co/u/Chia_Eng_Chang)\
**Post date:** [August 1, 2014, 10:09pm UTC](https://discuss.elastic.co/t/curl-xget-works-but-fail-when-use-restful-client-in-java/19039/4 "2014-08-01T22:09:02Z")

</div>

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&](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.
> 
> [Create Very Simple Jersey REST Service and Send JSON Data From Java Client • Crunchify](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](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](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](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](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](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](mailto: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](https://groups.google.com/d/msgid/elasticsearch/c5f9d940-4301-4423-9861-3dbb6da781cb%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:11am UTC](https://discuss.elastic.co/t/curl-xget-works-but-fail-when-use-restful-client-in-java/19039/5 "2017-07-06T01:11:30Z")

</div>


