Nodejs http client example
![nodejs http client example nodejs http client example](https://i.stack.imgur.com/fKF6x.png)
Libraries like Axios, SuperAgent, and Unirest were built to provide a simplistic and high level way of sending requests. You need to (locally) handle personal data and the code itself is verbose which can result in an error prone application. While the HTTP module works properly, making get requests is complicated. In NodeJS, several methods that provide this functionality, starting with the http(s) module provided in the standard library. HTTP get requests are used to fetch data from servers usually through APIs. fetch(url)Īlternatively, we could use the (cleaner) async-await version. To make a HTTP GET request, use the following code. With the update of Node.js to version 17.5, you no longer have to install a library like node-fetch to make HTTP get requests in Node.js. Unirest also allows us to chain query parameters using query() instead of manually adding them to the URL you are requesting resources from. The main difference between unirest and other libraries like axios and SuperAgent is that unirest is available in multiple languages such as JavaScript, Ruby, Java, and PHP. You can also use another simple HTTP client library that allows node.js applications to easily make requests. The response body contains the data sent back from the API and can be accessed through res.body(). With SuperAgent you can add request parameters to the URL by chaining them through query(). To rewrite the same request we have been making using SuperAgent, modify your request, as follows. Like Axios, SuperAgent is an API that provides a simple and flexible solution to making requests in NodeJS.
![nodejs http client example nodejs http client example](https://images.idgesg.net/images/article/2017/07/nodejs-server_lg-100729813-large.jpg)
In case of an error, the catch block handles it. Also, note that we are not parsing the response. It returns the response, from which we get the data res.data and log to the console.Īs you can see the Axios implementation requires less code to make the HTTP get request and process the data. We pass the url to axios which handles the request (with SSL implementations) for us. The above code can now be rewritten as shown below: const axios = require('axios') To install Axios, run the following command in your terminal. Writing promise-based requests is beneficial especially when dealing with large blocks of data since the rest of your application can continue executing as you wait for the request to resolve. The solution is to use third-party packages like Axios, SuperAgent, and Unirest.Īxios is a promise-based HTTP client that makes it easier to perform requests in NodeJS. While this way of making GET requests works, it is very verbose. That’s how you use https to make GET requests. The next step is to parse the data once the request completes. That is why we store every chunk in the rawData variable. Inside the callback function, the data will be received in chunks. If the GET request fails, the error message will be displayed on the console. Next, we call the https.get() method and pass in the URL stated and a callback function that will handle the response from the URL. In the above code, we start by first assigning the URL that will receive the request to the URL variable. In get-request.js, add the following code. http.get() returns http.ClientRequest()įor this tutorial, you will send your requests to the NASA photo of the day API. Http.get() accepts a URL which is a string, an object of options, and a callback function. This is the syntax for using the http(s) module for a GET request. So, let's change http to https before making GET requests to the API.