Evgeny Krivosheev Evgeny Krivosheev -4 years ago 91
Node.js Question

Got "Bad request " HTTPS Post request in NodeJS

I am trying to send Https post request using NodeJS, but getting back 'Bad request'. At the same time when I send the same request via curl everything is fine. Can you help to fix the Node code:

var options = {
host: 'api.wit.ai',
port: 443,
path: '/converse?v=20170611&session_id=125abc&q=Hi',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer <token>'
var req = https.request(options, function(res) {...}

The curl query:
curl -XPOST 'https://api.wit.ai/converse?v=20170611&session_id=125abc&q=Hi' \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H 'Authorization: Bearer <token>'

Answer Source

With http.request() your post data is not to be sent in the query string. If you include a query string, it is sent as a query string. Your post data needs to be sent with req.write(data). See the code example in the doc.

Probably your server is returning that error because there is no data in the body of the POST.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download