Rust, Software Development

How to make HTTP Requests in Rust lang?

Applications do not solely rely on databases or end-users as their primary data source.  This fact is no less actual in complex applications. We can retrieve data from (or send to) a multitude of other sources like SFTP, queues, and even APIs over HTTP. This post shows how to make HTTP requests in RUST using the reqwest crate, a popular crate for making HTTP requests, and providing a convenient and easy-to-use API for working with HTTP requests and responses.

Rust Lang HTTP Client Crate

To use the reqwest crate, we need to include it as a dependency in our Cargo.toml file. Consider the following example.

Our configuration adds the reqwest crate as a dependency in our project and specifies the version 0.11.13 we want to use. Also, we added a dependency to tokio for asynchronous runtime purposes. Once we have added the reqwest crate as a dependency, we can start using it in our code. Here is a simple asynchronous example using reqwest to make an HTTP GET request in Rust:

Let’s go through the Rust codes.

First, the application creates an instance of an HTTP client via Client::new() method. Next, it calls the get() method to prepare the HTTP request using the https://turreta.com/blog URL. Then, it uses the send() method to send the HTTP request and wait for the response.

Finally, the application prints the HTTP status code of the response using the status() method. 

As we can see, the Rust lang code is a straightforward example of using the reqwest crate to make HTTP requests. In addition to using the get method for HTTP GET requests, we can also use the create to make other HTTP requests, such as POST, PUT, and DELETE requests. For more information, consult the reqwest crate documentation on crates.io.

When we run the code, we get the following output.

Other HTTP Clients

In addition to the reqwest crate, we can use several other crates to make HTTP requests in Rust. Some of the most popular alternatives to reqwest include the following:

  • The hyper crate is a high-performance HTTP client and server library that provides a low-level API for working with HTTP requests and responses.
  • The surf crate is a lightweight and easy-to-use HTTP client library that provides a simple and intuitive API for making HTTP requests in Rust.
  • The tide crate is a web framework that provides a convenient and easy-to-use API for working with HTTP requests and responses.

These are just a few crates we can use as HTTP client libraries and web frameworks available for Rust. We can find a more comprehensive list of options on the Rust website or crates.io.

The choice of which HTTP client library or web framework (e.g., Actix-Web or Rocket) to use in Rust is a matter of personal preference. Also, the specific requirements of our project matter. We can try different options and see which works best for our needs.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like