将带括号的URL传递给curl

jyztefdp  于 2022-11-13  发布在  其他
关注(0)|答案(4)|浏览(284)

如果我尝试将包含括号的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之前需要对字符进行转义的描述?

4ngedf3f

4ngedf3f1#

-g添加到命令中:

-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 curl itself interpret them.
      Note that these letters are not normal legal URL contents but they should be encoded
      according to the URI standard.

      Example:
       curl -g "https://example.com/{[]}}}}"

curl.se/docs/manpage.html#-g

jhkqcmku

jhkqcmku2#

Globbing使用方括号,因此需要使用斜杠\对其进行转义。或者,以下命令行开关将禁用通配符:
--globoff(或短选项版本:-g
例如:

curl --globoff https://www.google.com?test[]=1
9q78igpj

9q78igpj3#

虽然我的URL中没有(明显的)括号,但我还是收到了这个错误,在我的情况下,--globoff命令不能解决这个问题。
例如(在iTerm 2中的mac上执行此操作):

for endpoint in $(grep some_string output.txt); do curl "http://1.2.3.4/api/v1/${endpoint}" ; done

我将grep的别名设置为“grep --color=always”。因此,上面的命令将导致以下错误,some_string将以grep设置为的任何颜色突出显示:

curl: (3) bad range in URL position 31:
http://1.2.3.4/api/v1/lalalasome_stringlalala

当在终端中查看时,终端将[colour\codes]some_string[colour\codes]透明地转换为预期的无特殊字符URL,但在后台,颜色代码在传递给curl的URL中发送,导致您的URL中出现括号。
解决方案是不使用匹配突出显示。

dwthyt8l

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

相关问题