TPPZ
TPPZ

Reputation: 4901

URL encode cURL parameters for Elasticsearch

I would like to make cURL/Elasticsearch understand HTTP query parameters passed as normal strings while being url encoded by the command.

If I run this HTTP GET via cURL to submit the query to Elasticsearch:

curl \
  -H 'Content-Type: application/json' \
  -XGET '127.0.0.1:9200/movies/movie/_search?q=%2Byear%3A%3E1980+%2Btitle%3Astar%20wars&pretty'

Then I am able to retrieve the expected documents.

However if I run this cURL query:

curl \
   -H 'Content-Type: application/json' \
   --data-urlencode "pretty" \
   --data-urlencode "q=+year:>1980 +title:star wars&pretty" \
   -XGET '127.0.0.1:9200/movies/movie/_search'

Then I get this error:

{
    "error": {
        "root_cause": [{
            "type": "json_parse_exception",
            "reason": "Unrecognized token 'pretty': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@7856627; line: 1, column: 8]"
        }],
        "type": "json_parse_exception",
        "reason": "Unrecognized token 'pretty': was expecting ('true', 'false' or 'null')\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@7856627; line: 1, column: 8]"
    },
    "status": 500
}

I am using:

Upvotes: 1

Views: 1862

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45432

--data-urlencode will send a POST and URL encode the body. You have to use -G or --get in order to send a GET request & append the data specified with --data-urlencode in the URL :

curl -G -v \
     -H 'Content-Type: application/json' \
     --data-urlencode "pretty=true" \
     --data-urlencode "q=+year:>1980 +title:star wars" \
     '127.0.0.1:9200/movies/movie/_search' 

Upvotes: 1

Related Questions