I'm having trouble using http
to make a call on a API
.
"{\"error\":\"Unexpected token d\",\"code\":400}"
const express = require('express');
const router = express.Router();
const http = require('https')
const config = {
workspace: 'xxxxxxxxxxxxx',
username: 'xxxxxxxxxxxxxx',
password: 'xxxxxxxxxx',
indentify: '123',
url: 'https://gateway.watsonplatform.net/conversation/api/v1/workspaces/'
}
config.urlMessage = config.url + config.workspace + '/message?version=2017-05-26'
let options = {
hostname: 'gateway.watsonplatform.net',
port: 443,
path: '/conversation/api/v1/workspaces/' + config.workspace + '/message?version=2017-05-26',
method: 'POST',
auth: config.username + ':' + config.password,
headers: {
'Content-Type': 'application/json'
}
}
router.get('/', (req, res, next) => {
let dados = {
input: {
text: req.query.texto
},
context: {
conversation_id: config.indentify,
system: {
dialog_stack: [{dialog_node: "root" }],
dialog_turn_counter: 1,
dialog_request_counter: 1
}
}
}
let Retorno = res
let reqWatson = http.request(options, (res) => {
console.log('ok')
res.setEncoding('utf8')
res.on('data', (chuck) => {
console.log('Result')
return Retorno.json(chuck)
})
})
reqWatson.on('error', (err) => {
console.log('Erro')
console.log(err)
})
reqWatson.write('data\n');
reqWatson.write('data\n');
reqWatson.end();
});
module.exports = router;