简体   繁体   English

PowerShell JSON 标头未正确转换

[英]PowerShell JSON Headers not Converting correctly

I am using an API REST call, but the problem is that it for some reason is not passing the header value correctly.我正在使用 API REST 调用,但问题是它由于某种原因没有正确传递标头值。 I am getting an error about it not converting from "System.String" to "System.Collections.IDictionary" .我收到关于它没有从"System.String"转换为"System.Collections.IDictionary" The code is:代码是:

$Headers = New-Object 'System.Collections.Generic.Dictionary[[String],[String]]'

$Headers.Add('X-CENTRIFY-NATIVE-CLIENT', 'true')
$Headers.Add('Content-Type', 'application/json')

$Body = @{
    TenantId = 'ID'
    User = 'cloudadmin@andrew1.com'
    Version = '1.0' 
} 

#$wr = Invoke-WebRequest -Method Post -Uri $url -Headers $Headers -Body $Body -Verbose  

Invoke-RestMethod -Uri "https://uri/Security/StartAuthentication" -Method Post  -Headers ($Headers | ConvertTo-Json -Compress) -UseBasicParsing -Body $Body 

But when I execute I get this error (FQID):但是当我执行我得到这个错误(FQID):

Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "{
    "X-CENTRIFY-NATIVE-CLIENT":  "true",
    "Content-Type":  "application/json"
}" value of type "System.String" to type "System.Collections.IDictionary".
At line:31 char:109
+ ... tication" -Method Post  -Headers ($Headers1 | ConvertTo-Json) -UseBas ...
+                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I have tried my headers being like this as well:我也试过我的标题是这样的:

$headers = @{

    'Content-Type'= 'application/json'
    'X-CENTRIFY-NATIVE-CLIENT'= 'true'
}

But I still get that same error.但我仍然遇到同样的错误。 It is odd that it keeps complaining about this reference;奇怪的是,它一直在抱怨这个引用; This library is not native to PoSH.这个库不是 PoSH 原生的。 Is there a DLL I should load or is there a better way to go about this?是否有我应该加载的 DLL 或者有更好的方法来解决这个问题?

The -Headers parameter expects a dictionary, not a json object. -Headers参数需要一个字典,而不是一个 json 对象。

Pass $Headers directly:直接传递$Headers

$uri = "https://uri/Security/StartAuthentication"
Invoke-RestMethod -Uri $uri -Method Post -Headers $Headers -UseBasicParsing -Body $Body 

You can inspect parameter details with Get-Help :您可以使用Get-Help检查参数详细信息:

PS > Get-Help Invoke-WebRequest -Parameter Headers

-Headers <IDictionary>

    Required?                    false
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           (All)
    Aliases                      None
    Dynamic?                     false

I took the json conversion out of the header on your invoke restmethod.我从你的调用 restmethod 的标题中取出了 json 转换。 That part will need to be done on your payload $Body .这部分需要在您的有效负载$Body Give this a try.试试这个。

$Headers = @{}
$Headers.Add('X-CENTRIFY-NATIVE-CLIENT', 'true')
$Headers.Add('Content-Type', 'application/json')

$Body = @{
    TenantId = 'ID'
    User = 'cloudadmin@andrew1.com'
    Version = '1.0' 
} 

$Body = ($Body | ConvertTo-Json)

#$wr = Invoke-WebRequest -Method Post -Uri $url -Headers $Headers -Body $Body -Verbose  

Invoke-RestMethod -Uri "https://uri/Security/StartAuthentication" -Method Post -Headers $Headers -UseBasicParsing -Body $Body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM