Chidume Nnamdi

Translator: UC International research and development Jothy


Welcome to the “UC International Technology” public account, we will provide you with the client, server, algorithm, testing, data, front-end and other related high-quality technical articles, not limited to original and translation.

The interaction between client and server is very important in Web development, which makes client applications highly dynamic. The server retrieves the data and returns it. The page does not need to be reloaded, but directly uses the returned data to re-render part/whole of its content, or to manipulate the data.

The technology behind this is AJAX, implemented through an XMLHttpRequest instance. In order to improve the experience of using AJAX and XMLHttpRequest, the community has developed libraries that directly issue HTTP requests without dealing with AJAX and XMLHttpRequest.

This article will take you through five of the most popular HTTP libraries and see how they are implemented. I hope it saved you some time.

Tip: Avoid code duplication by synchronizing common components with the Bit share. Make code that does the same thing a shared component, and you can use it anywhere. Build faster!



Axios

Promise based HTTP client, available in browsers and Node.js

Axios is a Promise based HTTP library that can be used to make HTTP requests on Node.js and browsers, supporting all modern browsers, even IE8+!

advantages

  • Supports both Node.js and browsers

  • Supporting Promise API

  • Requests can be configured or cancelled

  • You can set a response timeout

  • Support for preventing cross-site request forgery (XSRF) attacks

  • You can intercept requests or responses that are not executed

  • Displays the upload progress

  • Widely used in React and Vue projects

    disadvantages

    • It’s a little tricky to use


    Superagent

    Improved Ajax – Works with the Node.js HTTP client

    Superagent is a lightweight, promise-based progressive AJAX API that is ideal for sending HTTP requests and receiving server responses. Like Axios, it works with Node and all modern browsers.

    Making an HTTP request with a Superagent is as simple as calling a method on a Request object:

    advantages

    • It has a plug-in ecosystem, and you can build plug-ins to do more

    • configurable

    • The HTTP request sending interface is friendly

    • You can add methods to the request chain

    • This works with browsers and Nodes

    • Display upload and download progress

    • Supports block transfer encoding

    • Support for old-style callbacks

    • A thriving plugin ecosystem that supports many common functions

    disadvantages

    • The API doesn’t meet any standards


    Request

    🏊🏾 Simplified HTTP request client

    Request provides a simplified way of making HTTP requests. You can make HTTP requests with less code than other HTTP libraries. It’s not Promise based, but if you need promises, you can introduce a request-Promise library that encapsulates the request as a Promise and returns it.

    advantages

    • Easy to use API

    disadvantages

    • Is not based on a Promise


    Fetch

    Fetch is the browser’s built-in API for sending requests, designed to replace XMLHttpRequest.


    advantages

    • Flexible and easy to use

    • Use Promise to avoid callback hell

    • Support for all modern browsers

    • Follow the Request-Response scheme

    • Simple and clear grammar

    • Support the React Native

    disadvantages

    • It is not supported on the server

    • Lack of development library highlights, such as cancellation requests

    • No built-in defaults, such as request mode, request header, request credentials.


    Supertest

    Superagent driver, a smooth SET of apis for testing node.js HTTP servers

    Supertest tests the Node.js HTTP server. The library is supported by SuperAgent, which combines its own API with SuperAgent’s underlying API to provide a concise HTTP test interface.

    advantages

    • The fluent API

    • Simple HTTP assertions

    • Can be mixed with different test suites such as Chai. Js and Mocha


    disadvantages

    • Browser not supported


    conclusion

    After analyzing popular HTTP libraries, you might ask, “Which one should I choose?”

    I would say that depending on your project, size, and target audience, each choice can be good or bad. If you choose the wrong library for the wrong specification, think about it and choose the right tool again.

    If you have any questions or feel the need to add, correct or delete any questions, please feel free to comment, email or direct private message to me. Thanks for reading! 👏


    The original address: https://blog.bitsrc.io/comparing-http-request-libraries-for-2019-7bedb1089c83? gi=89b097d4bfd1



    Good article recommendation:

    JavaScript extreme Performance quest: TC39 binary AST proposal

    What would your career look like if there was only one browser on the market?



    UC International Technology is committed to sharing high quality technical articles with you

    Please follow our official account and share this article with your friends