无法正确转义引号和大括号

62

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

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系统,出现的错误与引号、括号和通配符有关。我尝试通过在引号前面加上反斜杠进行转义,但没有成功。

5个回答

99
不要使用单引号。在字符串中转义任何双引号使用\
curl -X POST -d "{ \"method\" : \"account_info\", \"params\" : [ { \"account\" : \"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh\"} ] }" http://s1.ripple.com:51234

在Windows系统中,command.exe好像不支持单引号。虽然PowerShell支持单引号,但是在使用时仍然存在一些问题,因此最好的解决方案是根本不使用单引号。


2
你怎么在这里插入一个环境变量?类似这样 { "account/": $ACCOUNT_KEY } - Walter Martin Vargas-Pena

48

你可以使用curl -g来关闭通配符:

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

比起转义所有那些括号来说更加简单。


2
对我没用。转义可以解决问题,但关闭globbing仍然会导致400错误。尽管如此,在某些情况下这仍然是一个好的答案。 - SteveCinq

4

对于Windows用户:您可以下载Git Bash来运行下面的curl命令。

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

2

试试基础版发布一些内容。

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

0
在上面的回复中,需要注意数据以JSON格式指定,这需要在Alok答案中指定--header
也可以像这样以"url"格式定义:
curl -X POST --data "method=account_info&params=[…]" http://s1.ripple.com:51234

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接