Passing an API key with an HTTP header in cURL

I have an API proxy in Apigee that is authenticated using an API key. I pass the key with my HTTP request header using cURL using 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 change my policy to look for the key in the request parameter, not the header, it works fine. Did I miss something?

+5
source share
3 answers

Try the following:

 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 
+8
source

PowerShell simply does not resolve the variable in your url. You are trying to request a service in the URI http: // $ serverHost: 1234 / service, which will not work. You could do

$ serverHost = "myHost" $ service = "http: // $ serverHost`: 1234 / service" Invoke-WebRequest $ service -Method Get

+1
source

You can set curl: fooobar.com/questions/12732 / ...

Remove the existing curl alias by running the following command:

Remove-item alias:curl

Then your team will work:

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

0
source

Source: https://habr.com/ru/post/1260430/


All Articles