22

I have an API Proxy in Apigee which is authenticated with an API key. I'm passing the key with my HTTP request header using cURL, with this command:

curl -v -H "apikey: my_key" http://api_org-test.apigee.net/v1/helloapikey

I get this error:

Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the 
"apikey: my_key" value of type "System.String" to 
type "System.Collections.IDictionary".

When I modify my policy to look for the key in query parameter rather than the header, it works fine. Am I missing something here?

kuk_94
  • 373
  • 1
  • 4
  • 18

5 Answers5

33

Try this:

curl -v -H @{'apikey' = 'my_key'} http://api_org-test.apigee.net/v1/helloapikey

Note: curl is an alias for the Invoke-WebRequest cmdlet:

Get-Alias curl

output:

CommandType     Name
-----------     ----
Alias           curl -> Invoke-WebRequest 
Martin Brandl
  • 47,498
  • 11
  • 97
  • 129
  • For multiple headers, multiple `-H` does not work, you need to use `-H @{'Cookies' = 'examplecookie=examplevalue'; 'Authorization: example'}` – Luc Apr 22 '20 at 15:24
9

You could install curl: https://stackoverflow.com/a/16216825/3013633

Remove existing curl alias by executing this command:

Remove-item alias:curl

Then your command will work:

curl -v -H "apikey: my_key" http://api_org-test.apigee.net/v1/helloapikey

iaforek
  • 1,906
  • 2
  • 27
  • 45
2

None of the above answers worked for me (I got an error -- parse error near }).

This worked:

curl -X GET \
  'http://api_org-test.apigee.net/v1/helloapikey' \
  -H 'apikey: my_key'
him229
  • 935
  • 1
  • 7
  • 18
1

PowerShell simply does not resolve the variable within your URL. You are trying to query the service at the URI http://$serverHost:1234/service which won't work. You could do

$serverHost = "myHost"
$service = "http://$serverHost`:1234/service"
Invoke-WebRequest $service -Method Get
Yatin
  • 2,348
  • 6
  • 20
  • 38
user1012506
  • 1,831
  • 19
  • 36
0

Just to add this to the discussion, I had to both hash the api key, but leave the token call key phrase rather than change it to 'apikey'. That's just what worked for me!

curl -v -H @{'X-API-TOKEN' = '[*insert key here*]'} '*datacenter_url*)'

Also noteworthy to PowerShell newcomers, -v stands for verbose. This switch gives you a Cyan-colored text under the command in PowerShell ise about the command PS is running. Almost like a play-by-play commentary. Useful enough I thought I'd mention it.