通过 Node.js API 创建主题

我试图通过 Node.js API 创建一个新主题,发送的负载如下:

 { title: 'topic:329201',
  raw: 'topic-description:32920111948',
  created_at: '2019-05-08',
  api_key:
   '2371b6549b0c3aeb1f12e49c22db5f25fb1f8ca740619e5304f12f4d29896097',
  api_username: 'cb1' 
}

我使用 Node 的 HTTP 模块以以下选项调用 API:

{ host: '13.127.14.24',
  port: '3000',
  path: '/posts',
  method: 'POST',
  headers:
   { 'content-type': 'application/x-www-form-urlencoded',
     'content-length': 194,
     Accept: '*/*',
     Connection: 'keep-alive' }
 }

但返回的响应体中显示 [“BAD CSRF”]。有人能帮忙吗?

You should probably revoke that API key since you just posted it :wink:

This is usually because incorrect content-type encoding was specified in the request.

1 个赞

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.

2 个赞

Its neither working with Content-Type:application/x-www-form-urlencoded nor with ‘content-type’:‘application/json’.

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.

4 个赞

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.

1 个赞

Fwiw: HTTP header names are (should be) case insensitive per RFC2616 4.2.

1 个赞

Problem solved. Thanks to @david. I was sending api_key in the payload but needed to send it via HTTP headers. I owe you a Beer. :laughing:

2 个赞

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!

5 个赞

Vishal, would you mind sharing the Node code you’re using to make this request?

I’m stuck with a 404 error, no matter what I try.

@souljacker Sorry for the late reply. Were you able to get it working?

Blake,你的工程团队中是否有人可以指导我如何用 Node.js 发出相同的请求?如果你不想在这个 Python 线程中公开讨论,我们可以私信交流。非常感谢!

我已根据 Node.js 网站上的示例进行了调整,以演示如何使用 Node.js 向 Discourse 发送 POST 请求:

const http = require('http');
const crypto = require('crypto');
const querystring = require('querystring');

const title = crypto.randomBytes(6).toString("hex") + ' ' + crypto.randomBytes(3).toString("hex") + ' ' + crypto.randomBytes(4).toString("hex");
const raw = crypto.randomBytes(16).toString("hex") + ' ' + crypto.randomBytes(16).toString("hex") + ' ' + crypto.randomBytes(16).toString("hex");

const api_key = 'f2b43adc...';
const api_username = 'system';

var data = {
  title: title,
  raw: raw
};

data = JSON.stringify(data);

const options = {
  hostname: 'localhost',
  port: 3000,
  path: '/posts',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': data.length,
    'Api-Key': api_key,
    'Api-Username': api_username
  }
};

const req = http.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`)

  res.on('data', (d) => {
    process.stdout.write(d)
  })
});

req.on('error', (error) => {
  console.error(error)
});

req.write(data);
req.end();
4 个赞