curl to Rust
Convert curl commands to Rust code using the reqwest crate.
Load Preset Sample:
curl to Rust Converter
This tool converts curl commands into ready-to-run Rust code using the reqwest crate — the most popular HTTP client for Rust. Choose Async (tokio) for modern async/await code powered by the Tokio runtime, or Sync (blocking) for simpler synchronous code using reqwest::blocking. Supports common curl flags: -X (method), -H (headers), -d (request body), -u (basic auth), and -k (skip TLS verification). Cargo.toml dependencies are included as comments at the top.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Converting an API's curl example into reqwest for a Rust service.
- Reproducing a captured request inside an existing Rust client.
- Getting the builder chain and body type right.
- Choosing between the async and blocking reqwest APIs.
- Producing a snippet for a small command-line tool.
Frequently Asked Questions
- Do I need the blocking or the async reqwest?
- reqwest is async-first and needs a runtime such as tokio. If you just want a script, enable the "blocking" feature and use reqwest::blocking::Client, which runs its own runtime internally. Do not call the blocking API from inside an async context — it panics.
- Which Cargo features do I need?
- JSON support is behind the "json" feature, so serde_json integration and .json() are unavailable without it. TLS comes from default-tls (native-tls); switch to rustls-tls for a pure-Rust stack that cross-compiles cleanly.
- How do I handle the response?
- Everything returns Result, so you propagate with ? and handle the error. Note that a 404 is Ok — call .error_for_status() to turn a non-2xx status into an Err, which is the closest equivalent to curl --fail.
- How does curl's -k translate?
- Client::builder().danger_accept_invalid_certs(true). The danger_ prefix is deliberate: it is in the API name so it shows up in review. Prefer .add_root_certificate() with your own CA where possible.
- Should I build a new Client per request?
- No. Client holds the connection pool and is designed to be created once and cloned — clone is cheap because it is internally reference-counted. Constructing one per call throws away pooling and TLS session reuse.
- Which TLS backend is used?
- reqwest defaults to native-tls, which uses the platform's own store, but `rustls-tls` is a pure-Rust alternative that cross-compiles cleanly and avoids linking OpenSSL. That choice is the usual cause of a build failing on one machine and not another.
- How are errors handled idiomatically?
- Each call returns a `Result`, so the `?` operator propagates failures up to a function returning `Result<_, reqwest::Error>` or a boxed error. Note that a 4xx or 5xx is not an error — call `error_for_status()` to turn it into one.
Common errors and gotchas
- Using the async API without a runtime, which compiles and then does nothing at run time.
- Mixing the blocking client into an async context, which panics rather than blocking.
- Forgetting the `json` feature flag, without which the `.json()` helpers do not exist.
- Assuming a non-2xx is an `Err`, when only a transport failure is — check `status()`.
- Creating a client per request rather than cloning one, which rebuilds the TLS setup each time.
Related Developer Utilities tools
RegExp Tester
Test regular expressions and inspect matches locally.
Regex Visualizer
Visual regex pattern diagram with live match highlighting and capture group annotations.
Subnet Calculator
Compute CIDR subnets, usable hosts, and network ranges.
Cron Parser
Translate cron syntax into plain English.
URL Parser
Break a URL into protocol, host, path, and query parts.
HTML Previewer
Paste HTML and see it rendered live in a safe, sandboxed preview.
HTTP Status Code Reference
Search and look up every HTTP status code and its meaning.
MIME Type Lookup
Find the MIME type for a file extension, or the extensions for a MIME type.