0

I am using an API REST call, but the problem is that it for some reason is not passing the header value correctly. I am getting an error about it not converting from "System.String" to "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 = '[email protected]'
    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):

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. Is there a DLL I should load or is there a better way to go about this?

1
  • Remove ConvertTo-Json completely, just do -Headers $Headers Commented Mar 9, 2020 at 18:36

2 Answers 2

3

The -Headers parameter expects a dictionary, not a json object.

Pass $Headers directly:

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

You can inspect parameter details with 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
Sign up to request clarification or add additional context in comments.

Comments

0

I took the json conversion out of the header on your invoke restmethod. That part will need to be done on your payload $Body. Give this a try.

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

$Body = @{
    TenantId = 'ID'
    User = '[email protected]'
    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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.