在cURL中使用HTTP标头传递API密钥

kuk*_*_94 13 powershell curl apigee

我在Apigee中有一个API代理,它使用API​​密钥进行身份验证.我使用cURL使用我的HTTP请求标头传递密钥,使用以下命令:

curl -v -H "apikey: my_key" http://api_org-test.apigee.net/v1/helloapikey
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the 
"apikey: my_key" value of type "System.String" to 
type "System.Collections.IDictionary".
Run Code Online (Sandbox Code Playgroud)

当我修改我的策略以查找查询参数而不是标题中的键时,它可以正常工作.我在这里错过了什么吗?

Mar*_*ndl 24

试试这个:

curl -v -H @{'apikey' = 'my_key'} http://api_org-test.apigee.net/v1/helloapikey
Run Code Online (Sandbox Code Playgroud)

注意: curlInvoke-WebRequestcmdlet 的别名:

Get-Alias curl
Run Code Online (Sandbox Code Playgroud)

输出:

CommandType     Name
-----------     ----
Alias           curl -> Invoke-WebRequest 
Run Code Online (Sandbox Code Playgroud)

  • 对于多个 header,多个 `-H` 不起作用,需要使用 `-H @{'Cookies' = 'examplecookie=examplevalue'; '授权:示例'}` (4认同)

him*_*229 7

以上答案都不适合我(我收到错误 - parse error near })。

这有效:

curl -X GET \
  'http://api_org-test.apigee.net/v1/helloapikey' \
  -H 'apikey: my_key'
Run Code Online (Sandbox Code Playgroud)


iaf*_*rek 6

您可以安装curl:https : //stackoverflow.com/a/16216825/3013633

通过执行以下命令来删除现有的curl别名:

Remove-item alias:curl

然后您的命令将起作用:

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

  • 就我而言,curl已经安装。只需要删除别名。 (2认同)