cURL to Rust
Convert a curl command to Rust reqwest
About cURL to Rust
Translates a cURL command into async Rust for the reqwest crate on a tokio runtime, with the Cargo.toml dependencies listed as comments at the top. Headers chain as .header() calls, -u becomes .basic_auth(), -F fields go to .form(), and the response text is printed after .send().await?. The command is parsed in the browser, never on a server.
Category: API.
Frequently asked questions
Which crates does the Rust snippet depend on?
▾
reqwest 0.12 and tokio: the code starts with the Cargo.toml lines to add (reqwest with the json feature, tokio with full) and runs in an async main marked #[tokio::main]. Requests are awaited with .send().await?, and errors propagate as reqwest::Error.
How is a JSON body sent in the Rust code?
▾
The payload goes to .body() as an escaped string literal rather than .json(), so no serde types are needed. The current output leaves the Content-Type header out when the body is JSON, so add a Content-Type: application/json header with .header() if the API checks it.
How do -X, -u and -F map to reqwest methods?
▾
-X, or the POST implied by -d, selects .get(), .post(), .put(), .delete(), .patch() or .head(), and other verbs go through .request() with reqwest::Method::from_bytes. -u user:pass becomes .basic_auth(user, Some(pass)) next to an explicit Authorization header with the same value, which you can delete. -F fields are passed to .form() and sent URL-encoded; multipart needs reqwest's multipart feature and reqwest::multipart::Form.
Does the Rust request follow redirects or skip TLS checks?
▾
Redirects are followed by default, up to 10, so -L needs no translation. -k is not converted; to accept invalid certificates, build the client with Client::builder().danger_accept_invalid_certs(true) instead of Client::new().