When developing a JavaScript app, you may need to have them interact with various Application Programming Interfaces (APIs) and send or receive data. To do that, they need to make a HTTP request from the server-side resource.
Making HTTP requests may look complicated on the surface, but you shouldn’t worry about it too much. In fact, if you’re not quite sure how HTTP requests work, the good news is that there are three possible ways that you can learn to make an API call.
- XMLHttpRequest
Prior to JavaScript ES6, the only way to make an HTTP request was by using Ajax and its XMLHttpRequest built-in browser object. By using the GET method, you can make your app retrieve data from an API.
To successfully make a HTTP request using this method, you will first need to initialize a new XMLHttpRequest() then specify the URL endpoint of the API, and the HTTP method (i.e. GET). The open() method will tie the HTTP method and URL endpoint, and the send() method will deliver the request.
After that is done, you will need to log the response using the XMLHttpRequest.onreadystatechange property. To test that everything is working as intended, you can use a free online REST API that will generate fake data.
- Fetch
A newer way of making HTTP requests effective JavaScript ES6 onwards is by using the Fetch method. It is widely supported by most modern browsers except for Internet Explorer and some older browsers.
Unlike XMLHttpRequest, Fetch only requires one parameter: The URL endpoint. It will return a ‘promise’ containing the response, and the JSON can subsequently be extracted from it by using the json() method.
Generally the Fetch API is a powerful way to make HTTP requests. Its only downside is that it doesn’t handle errors very well, and you will need to create your own error handling function.
- Axios
To put it simply, Axios is an open-source JavaScript library that is designed to make HTTP requests from node.js or XMLHttpRequests easier. It is based on the ES6 ‘promise’ API, and is supported by most modern browsers.
Compared to the Fetch method, Axios has several major advantages, which is why it is often preferred by developers. Its main benefits are the fact that it automatically transforms the data to JSON format, and that it handles errors better than the Fetch method.
To use Axios, it first needs to be installed using a package manager such as npm or yarn. It can then be included in the HTML file, and the HTTP request can be sent using the axios.get() method.
Because the data will already be in the JSON format it does not need to be transformed, and it is possible to log the response immediately.
Final Words
While all of these three methods can be used to make an API call in your JavaScript app, you should carefully consider which one fits best. More importantly you should keep in mind that there are pros and cons of consuming REST API in JavaScript in the first place.
If you’re ready, you should try each method out for yourself. The code may seem a bit complicated at first, but you can find examples of the source code here.
It should be noted that there are other ways of making HTTP requests as well, but the three methods listed above are the most popular – for good reason. Right now Axios is the safest and most widely-used choice, but in some specific cases you may find the other methods preferable.



