One of the most important tasks that a programmer has to solve when developing web projects is the organization of data exchange between client and server parts of such projects. It might look like this: the user presses a button on a page that is open in the browser, in response, the system executes a request to the server, after which the server sends the page the requested data. In order to display such data, on the page, without reloading it, they are processed, after which the page is updated and the user gets what he needs.
The basis of this system interaction is the AJAX technology, within the framework of which an instance of the
XMLHttpRequest
object is used. In order to make it easier for programmers to work with AJAX and
XMLHttpRequest
, specialized libraries have been created that provide developers with convenient interfaces, eliminating the need to use low-level mechanisms.
')
The material, the translation of which we are publishing today, is devoted to the analysis of five popular tools for working with HTTP: Axios, Request, Superagent, Fetch and Supertest.
Axios
The
Axios library, designed to perform HTTP requests, is based on promises. It is suitable for use in the Node.js environment and in browser applications. The library supports all modern browsers, including IE8 +.
▍ Strengths
- Works in the Node.js environment and in browsers.
- Supports promises.
- Allows you to execute and cancel requests.
- Allows you to set the response timeout.
- Supports protection against XSRF attacks.
- Allows you to intercept requests and responses.
- Supports indication of the progress of data upload.
- Widely used in projects based on React and Vue.
▍ Weaknesses
- The library is quite difficult to use.
Superagent
The
Superagent library, like Axios, is suitable for Node.js and for modern browsers. It provides the developer with a simple and intuitive API, which is convenient to work with.
In order to execute an HTTP request using Superagent, it is enough to call the appropriate method of the
request
object:
request .get('') .then(res => log(res)) .catch(err => log(err))
▍ Strengths
- Supports plugins.
- Configurable.
- It has a nice interface for making HTTP requests.
- Supports multi-call chaining for query execution.
- Works in the Node.js environment and in browsers.
- Supports progress indication for uploading and downloading data.
- Supports chunked-transfer encoding mechanism.
- Supports callbacks.
- Many plugins have been developed for this library.
▍ Weaknesses
- It has a kind of API that does not adhere to any standards.
Request
The
Request library, in comparison with the previously discussed tools, is a simplified tool for executing HTTP requests. When using this library, you have to write less code than when working with other libraries. It does not use promises, but if you need this opportunity, you can use the
Request-Promise library, which implements a wrapper around the Request library and allows you to work with promises.
▍ Strengths
- An API that is easy to use.
▍ Weaknesses
- The library does not use promises.
Fetch
Fetch is, unlike other tools discussed in this review, not a library. This is a standard browser API, which is an alternative to
XMLHttpRequest
.
▍ Strengths
- Flexibility and ease of use.
- The use of promises, which avoids the “hell of callbacks”.
- Support all modern browsers.
- Adherence to the request-response approach.
- Simple and nice syntax.
- Supported in React Native.
▍ Weaknesses
- Does not work in server environment.
- Does not implement some of the features found in HTTP libraries, such as canceling a request.
- It does not contain built-in support for default parameters, like query mode, headers, credentials.
Supertest
The
Supertest library
is based on the Superagent library. It is intended for testing HTTP servers built on the basis of Node.js. Supertest gives developers access to their own API and to the low-level API provided by the Superagent library.
▍ Strengths
- It has a convenient API.
- Simplifies the construction of HTTP tests.
- It can be used in conjunction with libraries for testing projects like Chai.js and Mocha .
▍ Weaknesses
- Does not work in browsers.
Results
In this article, we looked at several popular HTTP tools that are useful to JS developers who create applications that use browser technologies and Node.js platforms. When selecting a base for the HTTP subsystem of a certain project, it is recommended to first try out several tools that look appropriate, and then make the final decision.
Dear readers! What HTTP libraries do you use?