URL 为 Elasticsearch 编码 cURL 个参数

URL encode cURL parameters for Elasticsearch

我想让 cURL/Elasticsearch 理解作为普通字符串传递的 HTTP 查询参数,同时被命令 url 编码。

如果我 运行 通过 cURL 此 HTTP GET 将查询提交到 Elasticsearch:

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

然后我就可以检索到预期的文件了。

但是,如果我 运行 这个 cURL 查询:

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'

然后我得到这个错误:

{
    "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
}

我正在使用:

--data-urlencode will send a POST and URL encode the body. You have to use -G or --get 以便发送 GET 请求并在 URL 中附加用 --data-urlencode 指定的数据:

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'