33

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
  • 483
  • 1
  • 5
  • 18

5 Answers5

46

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
  • 56,134
  • 13
  • 133
  • 172
  • 3
    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
16

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
  • 2,860
  • 5
  • 40
  • 56
7

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
  • 1,284
  • 1
  • 11
  • 21
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
user1012506
  • 2,048
  • 1
  • 26
  • 45
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.