Reputation: 1288
Is it possible to make a request in the controller? I have tried using the node.js http module but didn't have any succes. Is there any other method to do this?
Upvotes: 1
Views: 7873
Reputation: 181
For people coming from the future, request package has fully deprecated since Feb 11th, 2020.
The alternative choices that request has recommended can be found as following.
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Package Name | Bundle Size | API Style | Summary |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| node-fetch | 0.4kb | promise / stream | A light-weight module that brings window.fetch to Node.js |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| bent | 1kb | fp / promise / stream | Functional HTTP client w/ async/await |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| got | 48.4kb | promise / stream | Simplified HTTP requests |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| make-fetch-happen | 442kb | promise / stream | make-fetch-happen is a Node.js library that wraps node-fetch-npm with additional features node-fetch doesn’t intend to include, including HTTP Cache support, request pooling, proxies, retries, and more! |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| axios | 11.9kb | promise / stream | Promise based HTTP client for the browser and node.js |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| unfetch | 1kb | promise / stream | Tiny 500b fetch “barely-polyfill” |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| superagent | 18kb | chaining / promise | Small progressive client-side HTTP request library, and Node.js module with the same API, sporting many high-level HTTP client features |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tiny-json-http | 22kb | promise | Minimalist HTTP client for GET and POSTing JSON payloads |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| needle | 164kb | chaining / promise | The leanest and most handsome HTTP client in the Nodelands |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| urllib | 816kb | callback / promise | Help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more. |
+-------------------+-------------+-----------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Upvotes: 2
Reputation: 1288
Ok, I managed to solve this using another module 'request'. What I did:
Install the module in your project:
npm install -S request
And in you code you should have:
var request = require('request');
request.get({
url: <your url>
}, function(error, response, body) {
if (error) {
sails.log.error(error);
}
else {
sails.log.info(response);
sails.log.info(body);
}
});
Upvotes: 14
Reputation: 89
I wrap node's native https.get (or http.get) in a promise and call from controller like so:
const https = require('https')
// wrap node's https.get stream call as a promise
// note: assumes utf-8 encoded data payload to get.
async function getdata(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
res.setEncoding('utf8');
let data = '';
res.on('data', (chunk) => {
data = data + chunk;
});
res.on('end', () => {
resolve(data);
})
}).on('error', (e) => {
reject(e);
});
});
}
// Call from sails controller
module.exports = {
myaction: async function(req, res) {
let data;
try {
data = await getdata('https://example.com/index.html');
} catch (e) {
// handle it
}
...
}
}
Upvotes: 1