Error Unexpected token d Node

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;
    
asked by anonymous 08.02.2018 / 19:33

1 answer

0

I was able to solve my problem as follows

const express = require('express');
const router = express.Router();
const http = require('https')

const config = {
    workspace: '57441ee0-xxxxx-xxxx-xxxx-xxxxxxx',
    username: 'aaff67d7-xxxx-xxxx-xxxx-xxxxxx',
    password: 'HTdxxxxxxx',
    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',
    headers: {
        'Content-Type': 'application/json',
        "Authorization": "Basic " + new Buffer( config.username + ':' + config.password ).toString('base64')
    }
}


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) => { 
        let response = ''
        res.on('data', (chuck) => {
            console.log('Result')
            response += chuck
        })

        res.on('end', () => {
            response = JSON.parse(response)

            Retorno.send(response)
        })
    }).end()



});

module.exports = router;
    
08.02.2018 / 20:06