Convert Figma logo to code with AI

seanmonstar logoreqwest

An easy and powerful Rust HTTP Client

9,809
1,106
9,809
439

Top Related Projects

14,477

An HTTP library for Rust

21,546

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.

A Rust compiler front-end for IDEs

12,669

The Rust package manager

53,463

:crab: Small exercises to get you used to reading and writing Rust code!

Quick Overview

Reqwest is a powerful and user-friendly HTTP client library for Rust. It provides a simple interface for making HTTP requests, with support for both synchronous and asynchronous operations, making it suitable for a wide range of applications.

Pros

  • Easy to use API with intuitive methods for different HTTP verbs
  • Supports both synchronous and asynchronous requests
  • Built-in JSON serialization and deserialization
  • Automatic handling of redirects, cookies, and authentication

Cons

  • Learning curve for developers new to Rust's async ecosystem
  • Limited built-in support for more advanced HTTP features (e.g., WebSockets)
  • Dependency on the tokio runtime for async operations
  • Some users report occasional issues with SSL/TLS on certain platforms

Code Examples

Making a simple GET request:

use reqwest;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let resp = reqwest::get("https://www.example.com").await?;
    println!("Status: {}", resp.status());
    println!("Body: {}", resp.text().await?);
    Ok(())
}

Sending a POST request with JSON data:

use reqwest::Client;
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    let res = client.post("https://httpbin.org/post")
        .json(&json!({
            "key": "value",
            "foo": "bar"
        }))
        .send()
        .await?;
    
    println!("Response: {:?}", res.json::<serde_json::Value>().await?);
    Ok(())
}

Using custom headers and query parameters:

use reqwest::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    let res = client.get("https://api.github.com/repos/rust-lang/rust")
        .header("User-Agent", "Reqwest")
        .query(&[("sort", "updated")])
        .send()
        .await?;
    
    println!("Status: {}", res.status());
    println!("Headers: {:?}", res.headers());
    Ok(())
}

Getting Started

To use Reqwest in your Rust project, add the following to your Cargo.toml:

[dependencies]
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }

Then, in your Rust file:

use reqwest;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let body = reqwest::get("https://www.rust-lang.org")
        .await?
        .text()
        .await?;
    
    println!("Body: {}", body);
    Ok(())
}

This example fetches the content of the Rust website and prints it to the console.

Competitor Comparisons

14,477

An HTTP library for Rust

Pros of Hyper

  • Lower-level HTTP implementation, offering more control and flexibility
  • Designed for both client and server-side usage
  • Better performance for high-concurrency scenarios

Cons of Hyper

  • Steeper learning curve due to its lower-level nature
  • Requires more boilerplate code for simple use cases
  • Less built-in convenience features compared to Reqwest

Code Comparison

Hyper (client-side GET request):

let client = Client::new();
let res = client.get("http://example.com").await?;

Reqwest (client-side GET request):

let res = reqwest::get("http://example.com").await?;

Summary

Hyper is a lower-level HTTP implementation offering more control and flexibility, suitable for both client and server-side usage. It excels in high-concurrency scenarios but has a steeper learning curve and requires more boilerplate code.

Reqwest, built on top of Hyper, provides a higher-level, more user-friendly API for HTTP clients. It offers more convenience features and simpler usage for common tasks, making it easier for developers to quickly implement HTTP requests in their applications.

Choose Hyper for more control and performance in complex scenarios, or Reqwest for simpler, more straightforward HTTP client needs.

21,546

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.

Pros of actix-web

  • Full-featured web framework with routing, middleware, and WebSocket support
  • High performance and scalability, consistently ranking among the fastest web frameworks
  • Built-in async runtime (tokio) for efficient handling of concurrent requests

Cons of actix-web

  • Steeper learning curve due to its comprehensive feature set
  • More opinionated architecture, which may limit flexibility in some cases
  • Larger codebase and dependencies compared to simpler HTTP clients

Code Comparison

actix-web:

use actix_web::{get, App, HttpServer, Responder};

#[get("/")]
async fn hello() -> impl Responder {
    "Hello, World!"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(hello))
        .bind("127.0.0.1:8080")?
        .run()
        .await
}

reqwest:

use reqwest;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let resp = reqwest::get("https://www.rust-lang.org")
        .await?
        .text()
        .await?;
    println!("{}", resp);
    Ok(())
}

A Rust compiler front-end for IDEs

Pros of rust-analyzer

  • Provides advanced IDE support for Rust, enhancing developer productivity
  • Offers a wide range of features including code completion, go-to-definition, and refactoring
  • Actively maintained by the Rust community, ensuring regular updates and improvements

Cons of rust-analyzer

  • Larger project scope and complexity compared to reqwest
  • May require more system resources due to its comprehensive analysis capabilities
  • Steeper learning curve for contributors due to its intricate codebase

Code Comparison

rust-analyzer (simplified example):

fn main() {
    let file = parse_file("example.rs");
    let analysis = analyze_code(file);
    generate_diagnostics(analysis);
}

reqwest (simplified example):

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let response = reqwest::get("https://www.example.com")?;
    println!("Status: {}", response.status());
    Ok(())
}

While rust-analyzer focuses on code analysis and IDE support, reqwest is a specialized HTTP client library. The code examples demonstrate their different purposes: rust-analyzer parses and analyzes Rust code, while reqwest simplifies making HTTP requests in Rust applications.

12,669

The Rust package manager

Pros of Cargo

  • Comprehensive package management and build system for Rust projects
  • Integrated with the Rust ecosystem, providing seamless dependency management
  • Supports project scaffolding and custom build scripts

Cons of Cargo

  • Focused on package management rather than HTTP requests
  • Steeper learning curve for beginners due to its broader scope
  • Larger codebase and more complex architecture

Code Comparison

Cargo (project configuration):

[package]
name = "my_project"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]

[dependencies]
serde = "1.0"
tokio = { version = "1", features = ["full"] }

Reqwest (HTTP request):

let client = reqwest::Client::new();
let res = client.get("https://api.example.com")
    .send()
    .await?;
let body = res.text().await?;

Summary

Cargo is a comprehensive package manager and build tool for Rust projects, while Reqwest is a focused HTTP client library. Cargo provides broader project management capabilities, including dependency resolution and build configuration. Reqwest, on the other hand, offers a simpler API for making HTTP requests in Rust applications. The choice between them depends on the specific needs of your project: use Cargo for overall project management and Reqwest for HTTP-related functionality within your Rust code.

53,463

:crab: Small exercises to get you used to reading and writing Rust code!

Pros of rustlings

  • Educational focus: Designed to teach Rust through interactive exercises
  • Structured learning path: Gradually introduces Rust concepts
  • Immediate feedback: Provides instant validation of solutions

Cons of rustlings

  • Limited scope: Focuses on learning, not practical application
  • Less versatile: Not usable as a library in other projects
  • Smaller community: Less active development and fewer contributors

Code comparison

rustlings example:

fn main() {
    let x = 5;
    println!("x has the value {}", x);
}

reqwest example:

use reqwest;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let resp = reqwest::get("https://www.rust-lang.org").await?;
    println!("{:#?}", resp);
    Ok(())
}

Summary

rustlings is an educational tool designed to teach Rust through hands-on exercises, providing a structured learning path with immediate feedback. It's excellent for beginners but has limited practical application beyond learning.

reqwest, on the other hand, is a powerful HTTP client library for Rust, offering real-world functionality for making network requests. It's more versatile and has a larger, more active community, but lacks the educational focus of rustlings.

Choose rustlings if you're learning Rust and want guided exercises. Opt for reqwest if you need to make HTTP requests in your Rust projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

reqwest

crates.io Documentation MIT/Apache-2 licensed CI

An ergonomic, batteries-included HTTP Client for Rust.

  • Async and blocking Clients
  • Plain bodies, JSON, urlencoded, multipart
  • Customizable redirect policy
  • HTTP Proxies
  • HTTPS via system-native TLS (or optionally, rustls)
  • Cookie Store
  • WASM

Example

This asynchronous example uses Tokio and enables some optional features, so your Cargo.toml could look like this:

[dependencies]
reqwest = { version = "0.12", features = ["json"] }
tokio = { version = "1", features = ["full"] }

And then the code:

use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let resp = reqwest::get("https://httpbin.org/ip")
        .await?
        .json::<HashMap<String, String>>()
        .await?;
    println!("{resp:#?}");
    Ok(())
}

Commercial Support

For private advice, support, reviews, access to the maintainer, and the like, reach out for commercial support.

Requirements

On Linux:

  • OpenSSL with headers. See https://docs.rs/openssl for supported versions and more details. Alternatively you can enable the native-tls-vendored feature to compile a copy of OpenSSL.

On Windows and macOS:

  • Nothing.

Reqwest uses rust-native-tls, which will use the operating system TLS framework if available, meaning Windows and macOS. On Linux, it will use the available OpenSSL or fail to build if not found.

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Sponsors

Support this project by becoming a sponsor.