【发布时间】:2016-11-30 02:24:58
【问题描述】:
我正在尝试使用 PowerShell Invoke-RestMethod 将数据发布到 URL,但是我不断收到此消息:
+ $response.RawContent + ~ Unexpected token '' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken
我是否遗漏了某处的字符串文字或格式错误的引号?
$user = "username"
$pass = "password"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')
$uri = "http://myurl.com"
$params = @{
message = "hello world"
}
$response = Invoke-RestMethod -Method Post -Headers $headers -Uri $uri -Body $params
$response.RawContent
【问题讨论】:
-
-Method参数出现两次。这个对吗?$method是什么?请考虑edit发送您的问题并分享minimal reproducible example。 -
$method 使用 $method = "post" ;但是,在试图找出我的错误在哪里时,我将方法直接移到了响应中。我已经编辑了代码。仍然得到相同的意外令牌错误...
标签: rest powershell post