Reputation: 584
I am not very familiar with Node js and as well as dealing with http requests so pardon me if this is something obvious.
I am following the examples on this website:
$.ajax({
url: 'https://api.wit.ai/message',
data: {
'q': 'set an alarm in 10min',
'access_token' : 'MY_WIT_TOKEN'
},
dataType: 'jsonp',
method: 'GET',
success: function(response) {
console.log("success!", response);
}
});
I am trying to create the equivalent of this but in Node Js. I attempted to use 'node request' however my code is not working. I have attempted a lot of variations of this but to no avail.
Here is an example:
var request = require('request');
var url = 'https://api.wit.ai/message';
var data = {
'q': 'hello test123 trying to get entities from this message',
'access_token': 'MY_WIT_TOKEN'
};
request.get({ url: url, formData: data }, function (err, httpResponse, body) {
if (err) {
return console.error('post failed:', err);
}
console.log('Get successful! Server responded with:', body);
});
When I compile this code, my terminal replies with:
Something went wrong. We've been notified.
Upvotes: 2
Views: 1664
Reputation: 584
To anyone interested here is the answer using node request that worked for me.
var request = require('request');
var headers = {
'Authorization': 'Bearer <WIT_TOKEN>'
};
var options = {
url: 'https://api.wit.ai/message?v=20160607&q=hello',
headers: headers
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
Upvotes: 2
Reputation: 19212
Use http:
var http = require('http');
http.get({
host: 'api.wit.ai',
path: '/message'
}, function(response) {
var body = '';
// get all data from the stream
response.on('data', function(data) {
body += data;
});
response.on('end', function() {
// all data received
console.log(body)
});
});
Upvotes: 2