shell脚本中的curl请求不起作用

时间:2018-01-27 08:48:49

标签: curl solr solr6

我正在尝试从shell脚本执行curl命令。此命令可以从cli正常运行。但是,我需要在shell脚本中执行它。 curl命令应该根据查询删除solr上的记录。

curl -v  -o /dev/null  -w %{http_code}    -H Content-Type:text/xml -X POST   http://localhost:8983/solr/content/update?commit=true -d  <delete><query>playlist_id:["" TO *]</query></delete> 

Note: Unnecessary use of -X or --request, POST is already inferred.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8983 (#0)
> POST /solr/content/update?commit=true HTTP/1.1
> Host: localhost:8983
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type:text/xml
> Content-Length: 30
> 
} [30 bytes data]
* upload completely sent off: 30 out of 30 bytes
< HTTP/1.1 400 Bad Request
< Content-Type: application/xml; charset=UTF-8
< Content-Length: 488
< 
{ [488 bytes data]
100   518  100   488  100    30  63649   3912 --:--:-- --:--:-- --:--:-- 69714
* Connection #0 to host localhost left intact
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: TO/
* Could not resolve host: TO
* Closing connection 1
curl: (6) Could not resolve host: TO
curl: (3) [globbing] unmatched close brace/bracket in column 2
400000
status: '400000'
Critical error happened during solr data delete: Verify the state manually
Quitting now

我需要检索http状态代码以验证状态。执行以下shell脚本代码。

curl_command_tmp="http://localhost:8983/solr/content/update?commit=true -d <delete><query>playlist_id:["" TO *]</query></delete>"
    curl_command_tmp="curl -v  -o /dev/null  -w %{http_code}    -H Content-Type:text/xml -X POST   $curl_command_tmp"
            echo "Curl delete command $curl_command_tmp"
           status=`$curl_command_tmp`
            echo $status
            echo "status: '$status'"
           if [ "$status" != "200" ]; then
                     echo "Critical error happened during solr data delete: Verify the state manually"
                     echo "Quitting now"
                     exit 0
           fi

我尝试了不同的curl选项(例如-G, - data-urlencode)。但他们不起作用。我想如果我可以传递完整的请求体而不会被截断,那么它将起作用。但它在solr http请求体中的“TO”之前被截断。 我经历过各种论坛,但尚未解决。需要你的帮助来解决它

1 个答案:

答案 0 :(得分:0)

我在评论中尝试了这个建议,但它没有用。相反,我将数据(<delete><query>playlist_id:["" TO *]</query></delete>)放在文件中并像这样使用它:

-d @FILE_NAME

这没有任何缺陷。我的猜测是,如果数据具有curl无法在cli上处理的特殊字符,则使用文件。

注意: - 我没有在FILE中添加任何额外的引号或双引号。只需将实际需要发送的数据放入。