【问题标题】:Passing a URL with brackets to curl将带括号的 URL 传递给 curl
【发布时间】:2012-01-10 03:50:00
【问题描述】:

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

【问题讨论】:

    标签: url curl


    【解决方案1】:

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

    -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.
    

    【讨论】:

    • 对我来说,它不起作用。我必须在每个方括号之前添加一个 \
    • @jesusperaltac 我也一样,用 macOS
    • 对我来说它有效 - 在 OS X High Sierr、curl 7.54.0 (x86_64-apple-darwin17.0) libcurl/7.54.0 上。
    • @Jean @jesusperaltac 对我来说,如果命令是curl -L -o <local_file_name> -g <url>,它可以在 Windows 上运行
    • 为我工作的 CentOS 7.1。 curl 版本 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0)
    【解决方案2】:

    Globbing 使用括号,因此需要用斜线 \ 转义它们。或者,以下命令行开关将禁用通配:

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

    例如:

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

    【讨论】:

      【解决方案3】:

      虽然我的 URL 中没有(明显的)括号,但我收到了这个错误,在我的情况下,--globoff 命令无法解决问题。

      例如(在 iTerm2 中的 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
      

      在终端中查看时,终端透明地将 [color\codes]some_string[colour\codes] 转换为预期的无特殊字符 URL,但在幕后,颜色代码在传递给 curl 的 URL 中发送,从而在您的 URL 中产生括号。

      解决方案是不使用匹配突出显示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-15
        • 1970-01-01
        相关资源
        最近更新 更多