如果我尝试将包含括号的URL传递给curl,它会失败并显示错误:
$ curl 'http://www.google.com/?TEST[]=1'
curl: (3) [globbing] illegal character in range specification at pos 29
但是,如果我将两个括号都转义,它似乎可以工作:
$ curl 'http://www.google.com/?TEST\[\]=1'
我该如何解决这个问题?是否有一个参数可以自动转义URL,或者在传递给curl之前需要对字符进行转义的描述?
4条答案
按热度按时间4ngedf3f1#
将
-g
添加到命令中:curl.se/docs/manpage.html#-g
jhkqcmku2#
Globbing使用方括号,因此需要使用斜杠
\
对其进行转义。或者,以下命令行开关将禁用通配符:--globoff
(或短选项版本:-g
)例如:
9q78igpj3#
虽然我的URL中没有(明显的)括号,但我还是收到了这个错误,在我的情况下,--globoff命令不能解决这个问题。
例如(在iTerm 2中的mac上执行此操作):
我将grep的别名设置为“grep --color=always”。因此,上面的命令将导致以下错误,some_string将以grep设置为的任何颜色突出显示:
当在终端中查看时,终端将[colour\codes]some_string[colour\codes]透明地转换为预期的无特殊字符URL,但在后台,颜色代码在传递给curl的URL中发送,导致您的URL中出现括号。
解决方案是不使用匹配突出显示。
dwthyt8l4#
上面的答案对我都不起作用,我必须用
%5B
和%5D
替换所有的开/关括号。[ ---> %5B
与] ---> %5D
我最初的curl网址是这样的
https://test.com/computer/agent1/api/json?pretty=true&tree=executors[currentExecutable[url]]
现在我就这样用
https://test.com/computer/agent1/api/json?pretty=true&tree=executors%5BcurrentExecutable%5Burl%5D%5D