Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to hand headers in curl request in PowerShell windows

curl -X POST <myUrl> -H "authorization: Bearer <valid token>"

But when I send it I get exception – Cannot bind parameter ‘Headers’. Cannot convert the "authorization: Bearer " value of type "System.String" to type "System.Collections.IDictionary"

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

curl is aliased to the Invoke-WebRequest cmdlet in Windows PowerShell.

As the error message indicates, the -Headers parameter of said cmdlet accepts a dictionary of header key-value pairs.

To pass an Authorization header, you’d do:

Invoke-WebRequest -Uri "<uri goes here>" -Method Post -Headers @{ Authorization = 'Bearer ...' } -UseBasicParsing

(Note that I’m explicitly passing the -UseBasicParsing switch – if not, Windows PowerShell will attempt to parse any HTML response with Internet Explorer’s DOM rendering engine, which is probably not what you want in most cases)


If you need to pass headers with token-terminating characters (like -) in the name, qualify the key with ' quotation marks:

$headers = @{ 
  'Authorization' = 'Bearer ...'
  'Content-Type'  = 'application/json'
}

Invoke-WebRequest ... -Headers $headers

If header order is important, make sure you desclare the dictionary literal [ordered]:

$headers = [ordered]@{ 
  'Authorization' = 'Bearer ...'
  'Content-Type'  = 'application/json'
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading