传递带有方括号的URL以进行卷曲


305

如果我尝试将包含括号的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'

有趣的是,我使用反斜杠转义它以错误代码20497静默失败的第一个括号:

$ curl 'http://www.google.com/?TEST\[]=1'
$ echo $!
20497

我的问题是在一般情况下该如何解决?是否存在可以自动转义URL的参数,或者是否存在传递给curl之前需要转义的字符的描述?

Answers:


481

没关系,我在文档中找到了它:

-g/--globoff
              This  option  switches  off  the "URL globbing parser". When you set this option, you can
              specify URLs that contain the letters {}[] without having them being interpreted by  curl
              itself.  Note  that  these  letters  are not normal legal URL contents but they should be
              encoded according to the URI standard.

10
对我来说,它没有用。我有每个方括号前面添加\
jesusperaltac

@jesusperaltac对于我来说,也一样,与macOS
Jean

对我来说,它起作用了-在OS X High Sierr上,curl 7.54.0(x86_64-apple-darwin17.0)libcurl / 7.54.0。
阴影

1
@Jean @jesusperaltac对我来说,如果命令是curl -L -o <local_file_name> -g <url>
Steven Liang

为我工作的CentOS 7.1。Curl版本7.29.0(x86_64-redhat-linux-gnu)libcurl / 7.29.0)
PatS

0

globbing使用方括号,因此需要用斜杠将其转义\。或者,以下命令行开关将禁用通配符:

--globoff (或短版版本: -g

例如:

curl --globoff https://www.google.com?test[]=1
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.