向 http 请求添加授权 header 时,cURL 中的 -u 是什么

What is -u from cURL when adding an authorization header to an http request

我正在尝试测试 Mix Panel 的 API 端点之一。我正在使用 Postman 来执行此操作,在 Mix Panel 的文档中,他们使用 cURL 来向您展示如何发出请求。当输入请求的 URL 和 POST 数据时,它的工作原理是它到达了正确的位置,并告诉我需要通过添加授权来进行身份验证 header.我感到困惑的是, header 的关键应该是什么?在他们的 cURL 示例中,它的 -u API_SECRET,那么授权 header 密钥会是 'username' 吗?

来自文档

# this uses a test project API secret, replace ce08d087255d5ceec741819a57174ce5
# with your own API secret
curl https://mixpanel.com/api/2.0/jql \
    -u ce08d087255d5ceec741819a57174ce5: \
    --data-urlencode params='{"from_date":"2016-01-01", "to_date": "2016-01-07"}' \
    --data-urlencode script='function main(){ return Events(params).groupBy(["name"], mixpanel.reducer.count()) }'

如果我想创建一个 AJAX 查询,例如

$.ajax({
        method: 'POST',
        url: 'https://mixpanel.com/api/2.0/jql',
        data: {
            'params': '{"from_date":"2016-01-01", "to_date": "2016-01-07"}',
            'script': '\'function main(){ return Events(params).groupBy(["name"], mixpanel.reducer.count()) }\''
        },
        headers: {
            <WHAT GOES HERE>: API_SECRET
        }
        }).then(function success(response){
            console.log('SUCCESS');
            console.log(response)
        }, function error(response){
            console.log('There was an error running JQL');
            console.log(response.error)
});

在这种情况下,您的 API_SECRET 是用户名,没有密码。所以在没有任何 "username" 键的情况下使用 curl -u <API_SECRET>: 是正确的。

来自关于示例调用的 mixpanel 文档https://mixpanel.com/help/reference/data-export-api

Authorization steps The Data Export API accepts Basic access authentication over HTTPS as an authorization method. To make an authorized request, put your project's API Secret in the "username" field of the Basic access authentication header. Make sure you use HTTPS and not HTTP - our API rejects requests made over HTTP, since this sends your API Secret over the internet in plain text.

Examples Here's an example of a properly-authenticated request made with cURL:

curl https://mixpanel.com/api/2.0/segmentation/ \ -u YOUR_API_SECRET: \ -d from_date="2016-02-11" -d to_date="2016-02-11" -d event="Viewed Page"