# Can't verify CSRF token authenticity while creating topics

**URL:** <https://meta.discourse.org/t/cant-verify-csrf-token-authenticity-while-creating-topics/234331>\
**Category:** Development\
**Tags:** rest-api\
**Created:** [July 28, 2022, 10:31pm UTC](https://meta.discourse.org/t/cant-verify-csrf-token-authenticity-while-creating-topics/234331 "2022-07-28T22:31:22Z")\
**Posts on this page:** 2\
**Page:** 1

## AI-generated summary

In the Development category under the rest-api tag, [attj](/t/-/234331/1) reported a "BAD CSRF" error and a "Can't verify CSRF token authenticity" log message when attempting to create a new topic via a `curl` POST request to the `/posts` endpoint. Despite verifying that the API key and username were active and ruling out typos, the request failed. [Gaurav_Tewari](/t/-/234331/2) identified the root cause: the `Api-Key` and `Api-Username` parameters were incorrectly passed as form data (`--form`) rather than in the HTTP headers. The solution provided is to move these credentials into the request headers alongside the `Content-Type`, which resolves the 403 error.

<div class="post-metadata">

**Author:** ![attj](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@attj](https://meta.discourse.org/u/attj)\
**Post date:** [July 28, 2022, 10:31pm UTC](https://meta.discourse.org/t/cant-verify-csrf-token-authenticity-while-creating-topics/234331/1 "2022-07-28T22:31:22Z")

</div>

My curl looks as follows:

```plaintext
curl -X POST 'https://example.com/posts' \
-H "Content-Type: multipart/form-data;" \
--form 'Api-Key="redacted"' \
--form 'Api-Username="redacted"' \
--form 'title="Test topic"' \
--form 'raw="Hey this is a topic."'

```

I get a [“BAD CSRF”] reply and the production logs say Can’t verify CSRF token authenticity. I checked this topic [https://meta.discourse.org/t/bad-csrf-on-creating-new-topic-via-api/89345](https://meta.discourse.org/t/bad-csrf-on-creating-new-topic-via-api/89345) but it is basically caused by a typo so I guess there is something different here. I double checked the api-key and the username and they are still active on the website. I would appreciate any suggestions that you may have. Thanks!

---

<div class="post-metadata">

**Author:** ![Gaurav\_Tewari](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gaurav_tewari/32/260621_2.png) [@Gaurav\_Tewari](https://meta.discourse.org/u/Gaurav_Tewari)\
**Post date:** [November 4, 2022, 7:06pm UTC](https://meta.discourse.org/t/cant-verify-csrf-token-authenticity-while-creating-topics/234331/2 "2022-11-04T19:06:47Z")

</div>

Hi, Your Curl request is giving 403 BAD CSRF error because Api-Key and Api-Username are not passed in the header along with Content-Type. Do this change and it will work.

Thanks  
GT
