I've learnt dealing with ajax calls to exchange information from the server to the browser but now I'm having big troubles converting my code to a server-side node compatible JS using http requests. I've read different tutorials but I just can't adapt them to my code.
My simple JS / jQuery function is this:
function ajaxCall(data, php, callback) {
ax = $.ajax({
type: "POST",
data: data,
url: php,
success: function (raw_data) {
f = $.parseJSON(raw_data);
callback(f);
},
});
}
And I need to convert it to a pure JS version with http requests to use with node.js. Thanks for any help.
EDIT: I tried and tried, but without success. Here is the code I used, I just get lots of meaningless words on my console.log, perhaps you can correct it:
Version 1
var data = {"action": "update", "tN": 2155};
var request = require("request");
request.post({
url: 'http://example.com/PHP.php',
data: data,
}, function(error, response, body){
console.log(body);
}
);
Version 2
var request = require("request");
var options = {
method: 'POST',
uri: 'http://example.com/PHP.php',
data: {"action": "update", "tN": 2155},
};
request(options, function(error, response, body) {
if(error){
console.log(error);
}else{
console.log(response);
}
});
Use request.js
Example:
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
})
Documentation request.js
Related
I'm trying to access all repositories that have more than 5000 stars on Github. I've written this scraper to work with Node.js (it's running on a Cloud9 environment):
var request = require('request');
var fs = require('fs');
var options = {
url: 'https://api.github.com/repositories',
headers: {
'User-Agent': 'myusernamehere'
},
qs: {
stargazers: 5000
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response.headers);
fs.writeFile('output_teste.json', body, function (err) {
if (err) throw err;
console.log('It\'s saved!');
console.log(response.statusCode);
});
} else {
console.log(response.statusCode);
}
}
request(options, callback);
But the result is not all of the repositories, just the first page of all of them. How can I use pagination with the Request module? I've tried to find examples within the documentation, but they aren't that clear. Or do I need to do this with another library or maybe another language?
Thanks!
you should modify your querystring to include the value of "since". You can read more on the github documentation.
https://developer.github.com/v3/repos/#list-all-public-repositories
Sample URL with query string of since
https://api.github.com/repositories?since=364
You could use the pagination data provided in response.headers.link that's received when making calls to the GitHub API to find out if there are any more pages left for your call.
One approach is to loop through the pages until there are no more new pages left, at which point you can write to file and return from function.
On each loop you can add to the data that you already have by using concat (I assume that the response body is delivered as an array) and then passing on the data to the next function call.
I rewrote your code to include a basic implementation of such a technique:
var request = require('request');
var fs = require('fs');
var requestOptions = function(page) {
var url = 'https://api.github.com/repositories?page=' + page;
return {
url: url,
headers: {
'User-Agent': 'myusernamehere'
},
qs: {
stargazers: 5000
}
};
};
function doRequest(page, incomingRepos) {
request(requestOptions(page), function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response.headers);
var currentPageRepos = JSON.parse(body);
var joinedRepos = incomingRepos.concat(currentPageRepos);
var linkData = response.headers.link;
// if response does not include reference to next page
// then we have reached the last page and can save content and return
if (!(linkData.includes('rel="next"'))) {
fs.writeFile('output_teste.json', JSON.stringify(joinedRepos), function(err) {
if (err) throw err;
console.log('It\'s saved!');
});
return;
}
page++;
doRequest(page, joinedRepos);
} else {
console.log(response.statusCode);
}
});
}
doRequest(1, []);
Im trying to send a post request to an arduino with Node JS and the Request package:
var body = {
d:"5",
l:"6",
TOTAL_VOLUME: "75",
meterId: "9"
};
var options = {
url: 'http://'+'192.168.1.102'+'/configData',
timeout: 7000,
headers: {
'Content-type' : 'application/json',
'Content-length': JSON.stringify(body).length
},
json:true,
body: JSON.stringify(body)
};
request.post(options, function (error, response, body) {
//console.log(error);
//console.log(response);
console.log(body);
if (!error && response.statusCode == 200) {
console.log("Changed configuration succesfully. ");
// Request to enpoint to save changes in database
var options = {
url: 'http://'+'8.8.8.8:4000'+'/meter/'+meter.id+'/',
method: 'PUT',
timeout: 10000,
body: {
'tank_diameter': tank_diameter,
'tank_length':tank_length,
'tank_capacity': tank_capacity
}
};
/*request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
}
});*/
}
done();
}).on('error', function(err) {
console.log(err);
done();
});
The above code is how I send the data, However Im not able to get the data on the arduino.
This is the code on arduino:
server.on("/configData", HTTP_POST, [](){ // configData Seteo de Valores desde POST
StaticJsonBuffer<200> configBuffer;
JsonObject& configJson= configBuffer.parseObject(server.arg("plain"));
String l = configJson["l"];
String d = configJson["d"];
String meterId2 = configJson["meterId"];
String volumenTotal = configJson["TOTAL_VOLUME"];
LENGTH = l.toFloat();
HEIGHT = d.toFloat();
meterId = meterId2.toInt();
TOTAL_VOLUME = volumenTotal.toFloat();
// GUARDAR EN LA EEPROM
int EEadr = 0;
EEPROM.write(EEadr, HEIGHT);
EEPROM.commit();
EEadr = 10;
EEPROM.write(EEadr, LENGTH);
EEPROM.commit();
EEadr = 20;
EEPROM.write(EEadr, TOTAL_VOLUME);
EEPROM.commit();
EEadr = 30;
EEPROM.write(EEadr, meterId);
EEPROM.commit();
//SHOW ON SERIAL MONITOR
Serial.println("l= "+l);
Serial.println("d= "+d);
Serial.println("meterId2= "+meterId2);
Serial.println("TOTAL_VOLUME= "+volumenTotal);
server.send ( 200, "text/json", "{success:true}" );
});
The weird thing is that if I use curl like this:
curl -H "Content-type: application/json" -X POST -d "{l:\"55\", r:\"10\", meterId: \"2\"}" http://192.168.1.2
The arduino does receive the data correctly, so the problem is most likely on my Node JS request. Can anyone tell me what Im I doing wrong here?
UPDATE:
Ive checked the requests with wireshark, and it results that the curl request (the one that is working) is being sent as Line based text data. Can anyone tell me how can I send it the same way using Node JS and request?
In these type of situations you can check your request structure with applications like wireshark.
In this problem if you can see that you attach your hole json as a single string, because when you set json flag of request in request library it convert your body into json for you so now you have something like:
var options = {
body: JSON.stringfy(JSON.stringfy(body))
};
so you can correct your application by simply set following options:
var options = {
url: 'http://'+'www.goole.com'+'/configData',
timeout: 7000,
json:true,
body: body
};
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.
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)
});
});
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);
I'm trying to get image url, and then post it somehow(either with FormData)...
I've found some usefull info here
1, 2, 3 ...
I've tried several variants of code. but still cant' load image to server.
My code now :
var FormData = require('form-data');
var request = require('request');
var form = new FormData();
request.get(url, {encoding:null}, function (error, response, body) {
form.append('my_logo', body);
form.getLength(function(err,length){
console.log(length);
var r = request.post(upload_url, { headers: { 'content-length': length } }, function(err, httpResponse, body) {
if (err) console.log(+err)
var res = (typeof body == 'string') ? JSON.parse(body) : body;
console.log(res);
})
r._form = form
});
})
in response from Post request i receive information, that loaded undefined...
I use request and form-data library...
How could i do that right?
Maybe with native node.js code it will be better?
I created a small api to generate test data on the fly. Each call creates a fresh user and returns the created data.
To load the data I use the package request:
var flow = protractor.promise.controlFlow();
var result = flow.execute(function() {
var defer = protractor.promise.defer();
request('http://localhost/test/recipe/person', function (error, response, body) {
if (!error && response.statusCode === 200) {
defer.fulfill(JSON.parse(body));
}
});
return defer.promise;
});
To make any use of the retrieved data I have to resolve the promise and proceed the test script inside a callback:
result.then(function(data) {
element(by.model('username')).sendKeys(data.person.email);
element(by.model('password')).sendKeys('test');
$('button[type="submit"]').click();
});
I don't like this callback handling and the possible hell it will lead to. Besides that, Protractor is so good in hiding this messy callback handling thingy. So, the question is:
How to use the result of an async call?
At the end I would like to perform code like the following:
var flow = protractor.promise.controlFlow();
var result = flow.execute(function() {...});
element(by.model('username')).sendKeys(result.person.email);
//...
Any ideas?
You can either make the http request synchronous - In most cases that's a bad thing to do.
Or, you can insert the callback into the execute function:
var flow = protractor.promise.controlFlow();
var result = flow.execute(function() {
var defer = protractor.promise.defer();
request('http://localhost/test/recipe/person', function (error, response, body) {
if (!error && response.statusCode === 200) {
defer.fulfill(JSON.parse(body));
}
});
defer.promise.then(function(data) {
element(by.model('username')).sendKeys(data.person.email);
element(by.model('password')).sendKeys('test');
$('button[type="submit"]').click();
});
return defer.promise;
});
but result will stay a promise.
It worked for me:
var request = require('request');
var options = {
method: 'POST',
url: 'http://testurl.com/endpoint/test/',
headers: {'id': 'ABCD',
'sessionid': 'dummyId',
'Accept': 'application/json',
'Accept-Language': 'en-us'
},
body: '{ "pay_load": [] }'
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(body);
console.log(info);
}
}
request(options, callback);