【问题标题】:Axios post unauthorized error yet curl worksAxios 发布未经授权的错误,但 curl 有效
【发布时间】:2019-06-29 08:48:55
【问题描述】:

试图通过 axios 访问 YouTrack 的 API,但我收到一个未经授权的错误,而通过 curl 工作的相同参数。

卷曲:

curl -X GET \
'https://<my youtrack url>/api/issues' \
-H 'Authorization: Bearer perm:<my token>' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json'

axios:

const config = {
    headers: {
        'Content-Type': 'application/json',
        Authorization: 'Bearer perm:<my token>'
    },
    responseType: 'json',
};

axios.get('https://<my youtrack url>/api/issues', {}, config)
    .then((response) => {
        console.log(response.data);
    })
    .catch(e => {
        console.log('Error: ', e.response.data)
    });

curl 正确返回我的可用问题的 JSON,而我的 axios 调用返回错误

{error: "Unauthorized", error_description: ""}

谢谢

【问题讨论】:

    标签: rest axios youtrack youtrack-api


    【解决方案1】:

    将配置作为第二个参数发送,因为 GET 请求不需要正文

    axios.get('https://<my youtrack url>/api/issues', config)
        .then((response) => {
            console.log(response.data);
        })
        .catch(e => {
            console.log('Error: ', e.response.data)
        });
    

    【讨论】:

    • 我遇到了类似的问题。来自 auth0 的代码 sn-p 使用了axios.request;我将其更改为axios.post,它按预期工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 2020-04-10
    • 2021-03-07
    • 1970-01-01
    • 2019-05-13
    • 2017-10-27
    • 1970-01-01
    相关资源
    最近更新 更多