简体   繁体   English

axios授权标头仅适用于get请求

[英]axios Authorization headers only working with get requests

I have a simple api endpoint for sharing posts /api/v1/posts/{id}/share that requires authentication with a bearer token. 我有一个简单的api端点,用于共享需要使用承载令牌进行身份验证的帖子/api/v1/posts/{id}/share

I try to send a POST request like the following and it responds with a 401 我尝试发送如下所示的POST请求,并且响应为401

axios.post(`/api/v1/posts/${id}/share`,
        {
        "headers": {
                "Authorization":"Bearer "+token,
                "Content-Type": "application/x-www-form-urlencoded",
                "Accept": "application/json"
            },
        });

only when i changed it to accept a GET on the backend and changed the code to this did it work 只有当我将其更改为在后端接受GET并将代码更改为此时,它才起作用

axios.get(`/api/v1/posts/${id}/share`,
        {
        "headers": {
                "Authorization":"Bearer "+token,
                "Content-Type": "application/x-www-form-urlencoded",
                "Accept": "application/json"
            },
        });

I tested my endpoints with an http client (insomnia) so the problem does seem to be with axios. 我使用http客户端(失眠)测试了端点,因此问题似乎出在axios上。 why is this happening and how can I solve it? 为什么会发生这种情况,我该如何解决?

the server I'm using is apache and the backend framework is laravel 我正在使用的服务器是apache,后端框架是laravel

The root cause of this problem is: axios.post and axios.get have different syntax -- the 2nd parameter represents "config" in axios.get , but represents "data" in axios.post . 这个问题的根本原因是: axios.postaxios.get具有不同的语法-所述第二参数表示在“配置” axios.get ,但在表示“数据” axios.post That's why it works in GET but fails in POST. 这就是为什么它在GET中起作用但在POST中失败的原因。

According to axios document , these 2 syntax are: 根据axios 文档 ,这两种语法是:

axios.get(url[, config]) axios.get(URL [,config])

axios.post(url[, data[, config]]) axios.post(URL [,data [,config]])

Thus, when you send HTTP POST request with code: 因此,当您发送带有代码的HTTP POST请求时:

axios.post(`/api/v1/posts/${id}/share`,
        {
        "headers": {
                ...
            },
        });

The request is sent, with HTTP body as {"header":{...}} -- no request header is configured. 发送请求,HTTP 正文{"header":{...}} -未配置请求标头。

To make it work with axios.post , the headers config object should be passed as the 3rd parameter. 为了使其与axios.post一起axios.post ,应将headers配置对象作为第3个参数传递。 Such as: 如:

axios.post(`/api/v1/posts/${id}/share`,
        {}, // or whatever data you want to send.
        {
        "headers": {
                ...
            },
        });

so I solved this by using a different syntax 所以我通过使用不同的语法解决了这个问题

const options = {
        method:"post",
        "headers": {
            "Authorization":token,
            "Content-Type": 'application/x-www-form-urlencoded',
            "Accept": "application/json"
        },
        url:`/api/v1/posts/${id}/share`
    };

    axios(options);

I honestly cannot say what changed but here you go for anyone that sees this. 老实说,我不能说改变了什么,但是在这里您可以找到任何看到这一点的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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