Unexpected token node error

Asked

Viewed 139 times

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?

  • change 'Content-Type': 'application/json' for dataType : 'JSON'

  • @Jorge Error continues :/ but it returns to error 415 Unsupported Media Type

  • @Lucascosta Yes, using the Postman I have the return of JSON straight.

  • this using https even instead of http in require('https')?

  • 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/

  • @Lucascosta Yes, it is https even

Show 2 more comments

1 answer

0

I managed 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.