Crear un tema mediante la API usando Node.js

I am trying to create a new topic via Node.js API by sending the following payload :

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

I am hitting the API via HTTP module of node with the following options :

{ 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' }
 }

But I am getting [“BAD CSRF”] in body. Can anyone help.

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.

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.

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.

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.

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

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:

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!

Vishal, ¿te importaría compartir el código de Node que estás usando para hacer esta solicitud?

Estoy atascado con un error 404, sin importar lo que intente.

@souljacker Perdón por la respuesta tardía. ¿Lograste que funcionara?

Blake, ¿tienes alguien en tu equipo de ingeniería que también pueda guiarme sobre cómo hacer la misma solicitud con Node.js? Podemos hablar por mensaje directo si no quieres publicar eso en este hilo de Python. ¡Muchas gracias!

He adaptado el ejemplo del sitio web de Node.js para mostrar cómo enviar una solicitud POST a Discourse usando Node.js:

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();