使用PowerShell调用-RemMethod

时间:2016-11-29 14:58:03

标签: rest powershell post

我正在尝试使用PowerShells Invoke-RestMethod将数据发布到网址,但我不断收到此消息:

+ $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​

1 个答案:

答案 0 :(得分:0)

删除$ response.RawContent并且它有效。将其更改为echo $ response以查看输出。