THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
Sometimes, the Application requires calling a Remote or external API from a nodejs Application.
It covers the following items
Nodejs is server-side code based on npm libraries.
Consume REST API involves HTTP request of type GET/POST/DELETE/PATCH
It involves sending a request of json data and receiving the HTTP Response with
For calling any rest API, We need the following.
http
and HTTPS
are inbuilt modules, that can be used as server and client
If the request url is https
, We can use the https
module, otherwise, use HTTP module
Here is a syntax to make an HTTP url request
http.get( options, response callback);
http.post( options, responsecallback);
required
keywordget
method with Url and returns call back, which contains data
and end
listenersdata listener called when API return response successfull.
const http = require('https');
const url = "https://jsonplaceholder.typicode.com/posts";
http.get(url, function (response) {
let posts = "";
response.on("data", function (data) {
posts += data.toString();
});
response.on("end", function () {
console.log(posts.length);
console.log(posts.toString());
});
});
🧮 Tags
Recent posts
Learn Golang Tutorials - Rune Types Explained with examples How to display images in Hugo with examples | Hugo Image shortcode Nodejs package.json resolutions How to find Operating System username in NodeJS? How to convert Double to Integer or Integer to double in Dart| Flutter By ExampleRelated posts