使用Invoke-RestMethod调用REST API调用的反斜杠URL会生成错误

时间:2018-05-07 17:31:33

标签: rest powershell

我有一段代码如下:

$url = 'https://foo/accounts/thing%5CBSTNTZ-MTRFKR'
$request_content_type = 'application/xml'

$response_checkout = Invoke-RestMethod `
-Credential $credential `
-Method Get `
-Uri $url `
-ContentType $request_content_type

Invoke-RestMethod生成以下异常:

PSMessageDetails      : 
Exception             : System.Net.WebException: The remote server returned an error: (404) Not Found.
                           at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
                           at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
TargetObject          : System.Net.HttpWebRequest
CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
ErrorDetails          : Account not found: ?\thing
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

帐户名称(thing \ BSTNTZ-MTRFKR)中的反斜杠似乎是罪魁祸首。我使用[System.Uri]::EscapeUriString()使用%5c编码反斜杠。是否有其他技巧可以在URI中添加黑名单?

以下链接将带您进入我正在调用的API的API参考。

https://docops.ca.com/ca-privileged-identity-management/12-8/en/integrating/rest-based-api/checkout-an-account

0 个答案:

没有答案