【发布时间】:2018-09-27 09:39:27
【问题描述】:
我仍然在让 json 在 powershell 中使用 curl 命令时遇到问题。
即使是一个简单的将某些内容发布到弹性中的请求也会因错误而惨遭失败
意外的字符('D'(代码 68)):期望用双引号来开始字段名称
我已经将脚本简化为基础,只是为了尝试测试 curl 和 json,但仍然会失败
$curlExe = "h:\powershell\esb\elastic\curl\curl.exe"
$elasticdata = @{
timereceived = "test"
timesent = "testing"
name = "anon"
status = 0
}
$curldata = $elasticdata | convertto-json -Compress
$elasticoutput = "h:\powershell\esb\elastic\elastic.txt"
$elastichost = "http://localhost:9200/newtest20/filecopy/?pretty"
$elasticheader = "content-type: application/json"
$elamethod = "POST"
$curlargs = $elastichost,
'-X',$elamethod,
'-d',$curldata,
'-H',$elasticheader
& $curlexe @curlargs
【问题讨论】:
-
您的示例数据不包含任何大写字母
D,这会导致您的错误消息 - 很难猜出什么不是双引号。我建议您使用 pscx 中的EchoArgs.exe之类的工具来查看传递给 cURL 的内容 -
您是否尝试过在标志上使用双引号?
-
如果我逐字输入 $curldata 中的内容,然后转义 " 然后它进入命令正常
$curldata = '{\"timereceived\":\"test\",\"timsent\":\"testing\",\"name\":\"anon\",\"status\":0}' -
@IanRamsden 是的 windows 不喜欢那些单引号:stackoverflow.com/questions/31503754/…
标签: json powershell curl