简体   繁体   中英

Passing API key with HTTP header in cURL

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?

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 

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

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'

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
  1. Type curl in your powershell
  2. It will invoke a Uri section
  3. Type your uri like this

    http://apikey:key@your_url.com

It will give the output you expected. Cheers

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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