A user, Vishal_Kaushik, seeks help creating a topic via the Discourse API using Node.js, encountering a “BAD CSRF” error. The issue is resolved when david suggests sending the API key via HTTP headers instead of the payload. Blake confirms that header keys should be correctly capitalized (e.g., ‘Content-Type’), though michaeld notes header names are case-insensitive per RFC2616. Vishal_Kaushik confirms the solution and thanks the community. Blake shares a working Node.js example using the http module, including proper headers and JSON formatting, to create a post via the API.
Don’t worry dude. I am smart enough to randomly change some characters in it.
And can you specify what i should send in ‘content-type’, as I’ve tried a lot of things.
You will tend to get “BAD CSRF” if your api key has not been supplied correctly. The best way to send it is via HTTP headers. There are more details at the top of our api documentation.
You might need to have correct capitalization for your header keys. So ‘content-type’ needs to be changed to ‘Content-Type’. Same for ‘Content-Length’, but you probably don’t even need to specify that one.
Ohh good, glad you got it working! Yes, sending them in the HTTP headers is the correct way to do it now, but they should still work in the body of the POST. Because I’m trying to improve our error messaging when using the API is there anything you changed as well? Also would you be willing to share the actual node http request code? And how are you using something like querystring.stringify on your payload? Thanks!
Blake do you have someone on your engineering team who would also guide me how to make the same request with Node.js. Can talk via DM if you don’t want to post that in this Python thread. Thanks a lot!