0
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;
The end-point that is consuming resume a valid JSON?
– BrTkCa
change
'Content-Type': 'application/json'
fordataType : 'JSON'
– Jorge.M
@Jorge Error continues :/ but it returns to error 415
Unsupported Media Type
– Rafael Augusto
@Lucascosta Yes, using the
Postman
I have the return ofJSON
straight.– Rafael Augusto
this using
https
even instead ofhttp
inrequire('https')
?– BrTkCa
Could you show the JSON that it returns after logging in with password? I don’t know if that’s the case, but if you try to put the user and password in the variable url: user:password@https://gateway.watsonplatform.net/Conversation/api/v1/workspaces/
– RpgBoss
@Lucascosta Yes, it is
https
even– Rafael Augusto