【问题标题】:Invoke-RestMethod with PowerShell使用 PowerShell 调用 RestMethod
【发布时间】: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


【解决方案1】:

删除了 $response.RawContent 并且它可以工作。将其更改为 echo $response 以查看输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-29
    • 2019-01-09
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2018-01-29
    • 2019-07-28
    相关资源
    最近更新 更多