# 即使在设置了API密钥和用户名到我的请求头后，我仍然收到错误的CSRF

**URL:** https://meta.discourse.org/t/im-getting-bad-csrf-error-even-after-setting-api-keys-and-usernames-to-my-headers/224514
**Category:** General
**Created:** [2022年四月16日 19:43 UTC](https://meta.discourse.org/t/im-getting-bad-csrf-error-even-after-setting-api-keys-and-usernames-to-my-headers/224514 "2022-04-16T19:43:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Allen\_Chen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/allen_chen/32/256627_2.png) [@Allen\_Chen](https://meta.discourse.org/u/Allen_Chen)
#### Post date: [2022年四月16日 19:43 UTC](https://meta.discourse.org/t/im-getting-bad-csrf-error-even-after-setting-api-keys-and-usernames-to-my-headers/224514/1 "2022-04-16T19:43:07Z")

</div>

我正在尝试发出一个 POST 请求，我已经设置了它的标头和内容，但它没有给我正确的结果：

```plaintext
export default async function postNewTopic(req, res) {

    axios.post(`url/posts.json?title="${req.query.title}"&raw=${req.query.content}&category=1`, {

    headers: {

      "Api-Key": token ,

      "Api-Username": {My key}

    }})

    .then(function (response) {

      res.json(response.data);

    })

    .catch(function (error) {

      const err = error?.response?.data || error;

      res.status(error.status || error.statusCode || +error.code || 500).json(err)

    })

}

```

这是我的前端代码：

```plaintext
const createTopic = () => {

    axios.post(`api/post-new?title=${title}&raw=${content}&category=1`, {

        headers: {

          "Authorization": 'Bearer' + "",

          "Api-Key": token ,

          "Api-Username": ""

        }})

    .then(function (response) {

      res.json(response.data);

    })

    .catch(function (error) {

      const err = error?.response?.data || error;

      console.log(error.response.data)

    })

  }

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2022年四月16日 19:49 UTC](https://meta.discourse.org/t/im-getting-bad-csrf-error-even-after-setting-api-keys-and-usernames-to-my-headers/224514/2 "2022-04-16T19:49:56Z")

</div>

您的 API 密钥应该是 API 密钥，用户名应该是用户名。看起来您将 API 密钥作为用户名发送了？

---

<div class="post-metadata">

### Author: ![Allen\_Chen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/allen_chen/32/256627_2.png) [@Allen\_Chen](https://meta.discourse.org/u/Allen_Chen)
#### Post date: [2022年四月16日 20:00 UTC](https://meta.discourse.org/t/im-getting-bad-csrf-error-even-after-setting-api-keys-and-usernames-to-my-headers/224514/3 "2022-04-16T20:00:08Z")

</div>

不，不，它只在示例中，我已将其删除。但我已放入 api 密钥和 api 用户名，get 函数正在工作。
