【问题标题】:Curl request in shell script not workingshell脚本中的卷曲请求不起作用
【发布时间】:2018-01-27 08:48:48
【问题描述】:

我正在尝试从 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”之前被截断。 我已经浏览了各种论坛,但尚未解决。需要您的帮助来解决它

【问题讨论】:

  • 这里的双引号 ["" TO *] 混淆了脚本外壳。它认为第一个" 与命令开头的" 匹配,第二个"" 开始一个与命令末尾的" 匹配的新字符串。结果是"" 消失了。尝试使用反斜杠重写该表达式,以保护每个双引号不被视为字符串分隔符,例如[\"\" TO *]

标签: curl solr solr6


【解决方案1】:

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

-d @FILE_NAME

这没有任何缺陷。我的猜测是,如果数据具有 curl 无法在 cli 上处理的特殊字符,那么请改用 file。

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

【讨论】:

  • 你也可以用单引号包裹你的 XML。
猜你喜欢
  • 2011-08-10
  • 2017-11-29
  • 2016-04-16
  • 2011-10-27
  • 2017-04-13
  • 2021-02-24
  • 1970-01-01
  • 2014-11-11
  • 2018-09-20
相关资源
最近更新 更多