-1

In my Spring Boot app's REST controller, I receive a GET call from a Node.js app, parse the incoming query string, and construct a query for Elasticsearch (v 5.4)

The elasticsearch query string, which I have constructed looks like this:

http://34.230.72.180:9200/metadata/_search/?q="*desc*2*"

When I take this string, and submit it from a browser or Postman, I get the expected search results back.

However, when I submit the exactly same query from my REST controller:

HttpGet getRequest = new HttpGet(
                    finalReq);  //finalReq: http://34.230.72.180:9200/metadata/_search/?q="*desc*2*"
            getRequest.addHeader("accept", "application/json");

            HttpResponse response = httpClient.execute(getRequest);

I get this error:

INFO: Initializing Spring FrameworkServlet 'dispatcherServlet'
http://34.230.72.180:9200/metadata/_search/?q="*desc*2*"
Jan 06, 2018 12:33:42 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Illegal character in query at index 46: http://34.230.72.180:9200/metadata/_search/?q="*desc*2*"] with root cause
java.net.URISyntaxException: Illegal character in query at index 46: http://34.230.72.180:9200/metadata/_search/?q="*desc*2*"
    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:3111)
    at java.net.URI$Parser.parse(URI.java:3053)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)

What am I doing wrong here?

Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167

1 Answers1

1

You should encode your url paramters:

url = "http://34.230.72.180:9200/metadata/_search/?q=" + URLEncoder.encode("\"*desc*2*\"", "UTF-8");

Ref:

Encoding URL query parameters in Java

http://www.avajava.com/tutorials/lessons/how-do-i-encode-url-parameters.html

http://www.baeldung.com/java-url-encoding-decoding

shawn
  • 4,305
  • 1
  • 17
  • 25