Powershell Core Invoke-RestMethod 给出意外字符('M'(代码 77)):需要一个有效值
Powershell Core Invoke-RestMethod giving Unexpected character ('M' (code 77)): expected a valid value
这让我发疯了过去几天我一直试图在论坛中找到一些答案,但没有看到任何可以帮助我的东西。可能是我瞎了。
这是我的问题。我正在尝试使用提供的 API 更新我们的 Confluence Wiki 页面之一来更新页面。
我有三个脚本或函数:
- 调用 Create 的部署脚本或控制器脚本,并通过响应 (Json) 调用 Update
- 创建 Confluence Json
- 更新页面
这是创建函数
function Create-WikiPage
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[int]$CurrentPageRevisionNumber
)
# Creates the ID for new page
$NextPageID=$CurrentPageRevisionNumber + 1
# Creates the json body of the Wiki page
$ToolsPage= @{
"version"= @{
"number"= $NextPageID
};
"title"= "Windows Build Agent Tool Set";
"type"= "page";
"body"= @{
"storage"= @{
"value"= "<p><table><tr><th>Vendor-Application</th><th>Version</th></tr></table></p>";
"representation"= "storage"
}
}
} | ConvertTo-Json
$ToolsPage
}
更新函数如下所示:
Update-WikiPage {
[CmdletBinding()]
param(
[ValidateNotNullOrEmpty()]
[string]$Server,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Username,
[ValidateNotNullOrEmpty()]
[string]$Password,
[ValidateNotNullOrEmpty()]
[long]$PageId,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
$Data
)
[Net.ServicePointManager]::SecurityProtocol = 'Tls12, Tls11'
$Encoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($Username):$Password"))
$Header = @{"Authorization" = "Basic $Encoded"
}
Write-Information ($Data | Out-String)
$Data.GetType()
# Updates the Wiki page
Invoke-RestMethod "$($Server)/rest/api/content/$($PageId)" -Method PUT -Headers $Header -ContentType "application/json" -Body $Data -PreserveAuthorizationOnRedirect
}
如您所见,作为更新功能的一部分,我已经打印了 JSon 对象。这是打印出来的:
{
"version": {
"number": 9
},
"body": {
"storage": {
"value": "<p><table><tr><th>Vendor-Application</th><th>Version</th></tr></table></p>",
"representation": "storage"
}
},
"title": "Windows Build Agent Tool Set",
"type": "page"
}
这是我得到的 powershell 错误:
VERBOSE: received -byte response of content type application/json
Invoke-RestMethod : {"statusCode":500,"message":"org.codehaus.jackson.JsonParseException: Unexpected character ('M' (code 77)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: com.atlassian.confluence.plugins.restapi.filters.LimitingRequestFilter@6b6831ec; line: 1, column: 2]"}
At C:\Users\ChildsC\Documents\Git\PowerShellModules\Wiki\Update-WikiPage.ps1:65 char:2
+ Invoke-RestMethod "$($Server)/rest/api/content/$($PageId)" -Metho ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Method: PUT, Re...ication/json
}:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
编辑
我注意到当我从 Create Json -> Deploy -> Update 传递 json 对象时,他的问题发生了
如果我在 Deploy 脚本中创建 json 并将其传递给 Update,它可以正常工作。
好的
原来我是个白痴。没有人会看到这个问题,因为我没有 post 完整的脚本。
在创建函数的前面,我有一行 post params 到控制台:
$PsBoundParameters | Out-String
因此,当我认为脚本仅返回 $ToolsPage 值时,它实际上还返回了另一个对象。
因此更新脚本的原因是因为我明确传递了 $ToolsPage 变量。
我通过设置参数发现了这一点,它应该将对象的值保存为 $Global:variable_name,然后比较来自我的控制台的值。
这让我发疯了过去几天我一直试图在论坛中找到一些答案,但没有看到任何可以帮助我的东西。可能是我瞎了。
这是我的问题。我正在尝试使用提供的 API 更新我们的 Confluence Wiki 页面之一来更新页面。
我有三个脚本或函数:
- 调用 Create 的部署脚本或控制器脚本,并通过响应 (Json) 调用 Update
- 创建 Confluence Json
- 更新页面
这是创建函数
function Create-WikiPage
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[int]$CurrentPageRevisionNumber
)
# Creates the ID for new page
$NextPageID=$CurrentPageRevisionNumber + 1
# Creates the json body of the Wiki page
$ToolsPage= @{
"version"= @{
"number"= $NextPageID
};
"title"= "Windows Build Agent Tool Set";
"type"= "page";
"body"= @{
"storage"= @{
"value"= "<p><table><tr><th>Vendor-Application</th><th>Version</th></tr></table></p>";
"representation"= "storage"
}
}
} | ConvertTo-Json
$ToolsPage
}
更新函数如下所示:
Update-WikiPage {
[CmdletBinding()]
param(
[ValidateNotNullOrEmpty()]
[string]$Server,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Username,
[ValidateNotNullOrEmpty()]
[string]$Password,
[ValidateNotNullOrEmpty()]
[long]$PageId,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
$Data
)
[Net.ServicePointManager]::SecurityProtocol = 'Tls12, Tls11'
$Encoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($Username):$Password"))
$Header = @{"Authorization" = "Basic $Encoded"
}
Write-Information ($Data | Out-String)
$Data.GetType()
# Updates the Wiki page
Invoke-RestMethod "$($Server)/rest/api/content/$($PageId)" -Method PUT -Headers $Header -ContentType "application/json" -Body $Data -PreserveAuthorizationOnRedirect
}
如您所见,作为更新功能的一部分,我已经打印了 JSon 对象。这是打印出来的:
{
"version": {
"number": 9
},
"body": {
"storage": {
"value": "<p><table><tr><th>Vendor-Application</th><th>Version</th></tr></table></p>",
"representation": "storage"
}
},
"title": "Windows Build Agent Tool Set",
"type": "page"
}
这是我得到的 powershell 错误:
VERBOSE: received -byte response of content type application/json
Invoke-RestMethod : {"statusCode":500,"message":"org.codehaus.jackson.JsonParseException: Unexpected character ('M' (code 77)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: com.atlassian.confluence.plugins.restapi.filters.LimitingRequestFilter@6b6831ec; line: 1, column: 2]"}
At C:\Users\ChildsC\Documents\Git\PowerShellModules\Wiki\Update-WikiPage.ps1:65 char:2
+ Invoke-RestMethod "$($Server)/rest/api/content/$($PageId)" -Metho ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Method: PUT, Re...ication/json
}:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
编辑
我注意到当我从 Create Json -> Deploy -> Update 传递 json 对象时,他的问题发生了 如果我在 Deploy 脚本中创建 json 并将其传递给 Update,它可以正常工作。
好的
原来我是个白痴。没有人会看到这个问题,因为我没有 post 完整的脚本。
在创建函数的前面,我有一行 post params 到控制台:
$PsBoundParameters | Out-String
因此,当我认为脚本仅返回 $ToolsPage 值时,它实际上还返回了另一个对象。
因此更新脚本的原因是因为我明确传递了 $ToolsPage 变量。
我通过设置参数发现了这一点,它应该将对象的值保存为 $Global:variable_name,然后比较来自我的控制台的值。