【问题标题】:Having trouble escaping quotes and braces转义引号和大括号时遇到问题
【发布时间】:2013-07-07 19:19:44
【问题描述】:

我正在尝试在命令提示符中执行以下行:

curl -X POST -d '{ "method" : "account_info", "params" : [ { "account" : "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh"} ] }' http://s1.ripple.com:51234

但是,我得到以下信息:

curl: (6) Could not resolve host: method
curl: (7) Failed connect to :80; No error
curl: (6) Could not resolve host: account_info,
curl: (6) Could not resolve host: params
curl: (7) Failed connect to :80; No error
curl: (3) [globbing] illegal character in range specification at pos 2
curl: (3) [globbing] unmatched brace at pos 2
curl: (6) Could not resolve host: account
curl: (7) Failed connect to :80; No error
curl: (3) [globbing] unmatched close brace/bracket at pos 35
curl: (3) [globbing] unmatched close brace/bracket at pos 1
curl: (3) [globbing] unmatched close brace/bracket at pos 1
unable to parse request

我在 Windows 上,错误与引号、大括号和通配符有关。我尝试通过在引号前面加上反斜杠来转义引号,但没有成功。

【问题讨论】:

    标签: curl escaping command-prompt quotes curly-braces


    【解决方案1】:

    不要使用单引号。使用 \ 转义字符串中的任何双引号。

    curl -X POST -d "{ \"method\" : \"account_info\", \"params\" : [ { \"account\" : \"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh\"} ] }" http://s1.ripple.com:51234
    

    Window 的 command.exe 似乎不支持单引号。 PowerShell可以,但是在使用的时候还是会出现一些问题,所以最好的办法就是完全不使用。

    【讨论】:

    • 如何在其中插入环境变量?像 { \"account/": $ACCOUNT_KEY }
    【解决方案2】:

    您可以使用curl -g 关闭通配:

    curl -g -X POST -d '{ "method" : "account_info", "params" : [ { "account" : "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh"} ] }' http://s1.ripple.com:51234
    

    比转义所有这些括号更容易。

    【讨论】:

    • 对我不起作用。逃避工作,转向通配符仍然给我一个 400 错误。不过,在某些情况下,这仍然是一个很好的答案。
    【解决方案3】:

    尝试基本的发布内容。

    curl -X POST --data '{"username":"username", "password":"password"}' --header "Content-Type:application/json" http://127.0.0.1:8000/your_url/
    

    【讨论】:

      【解决方案4】:

      在上述回复中,需要注意的是数据是以 JSON 格式指定的,这需要将--header 指定为Alokanswer

      也可以这样定义为“url”格式:

      curl -X POST --data "method=account_info&params=[…]" http://s1.ripple.com:51234
      

      并避免指定--header "Content-Type…"

      【讨论】:

        【解决方案5】:

        对于 windows 用户:您可以下载 git bash 以运行以下 curl 命令。

        curl http://localhost:8080/subpage --include --header "Content-Type: application/json" --request "POST" --data '{"variable": "value"}
        

        【讨论】:

          猜你喜欢
          • 2019-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-15
          • 2021-02-22
          • 1970-01-01
          • 2019-06-28
          相关资源
          最近更新 更多