Hi I’m trying to add queries to this Post request in Node.js. I’m not sure how to do so.
Here is the Post request code that I am using,
var options = {
host: 'ws.ispeech.org',
port: 80,
path: '/api/rest/1.5',
method: 'POST',
};
var http = require('http');
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
A query string is part of the URL, which the node.js options object calls the “path”. So you can simply add the query string to the path:
Note that there is also a “querystring” module which will encode object propertly/values for you: