Send GraphQL with cURL in Rust
Paste a curl command that sends GraphQL and get idiomatic Rust code. We pick the right serialization for reqwest and set the correct Content-Type.
Quick answer
To convert a cURL command to Rust, paste it into the Toolpaper workbench below. It parses the command in your browser and emits idiomatic Rust code — headers, query string, request body, authentication (Bearer, Basic, API key), and cookies are all wired up automatically. No data is uploaded; conversion is fully client-side.
Prefer a different target? See the full cURL to Rust reference, or jump to the same guide in JavaScript, Node.js, or Python.
// Cargo.toml: reqwest = { version = "0.12", features = ["json", "blocking", "gzip", "brotli"] }
use reqwest::blocking::Client;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use std::str::FromStr;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::builder().redirect(reqwest::redirect::Policy::none()).build()?;
let mut headers = HeaderMap::new();
headers.insert(HeaderName::from_str("content-type")?, HeaderValue::from_str("application/json")?);
let mut req = client.post("https://api.example.com/graphql").headers(headers);
req = req.body("{\"query\":\"{ viewer { id name } }\"}");
let resp = req.send()?;
println!("{}", resp.text()?);
Ok(())
}Example
A representative curl command for this scenario. Paste your own above to convert an exact match.
curl -X POST https://api.example.com/graphql \
-H "Content-Type: application/json" \
-d '{"query":"{ viewer { id name } }"}'Implementation notes
- Content-Type is detected from -H headers first, then inferred from the body shape when the header is missing.
- GraphQL payloads keep their { query, variables, operationName } shape so persisted-query servers keep matching.
- Charset directives on Content-Type (e.g. application/json; charset=utf-8) survive the round trip.
Common mistakes
- Setting Content-Type manually and then also using reqwest's helper — you end up with duplicate headers or the wrong one wins.
- Mixing form encoding with JSON headers — the server rejects with 400 and it's easy to miss in code review.
- Trusting the browser DevTools "Copy as cURL" to preserve charset — it often drops it. Clean panel restores common defaults.
Common questions
- How is GraphQL encoded in the generated Rust?
- GraphQL queries are treated as JSON payloads with the { query, variables } shape intact.
- Does it preserve Content-Type?
- Yes. The Content-Type header is honored and matched against the body so serialization stays consistent.
- Is anything uploaded?
- No. Parsing and generation happen entirely in your browser.
More payload types in Rust
Rust hub →The same guide in other languages
All guides →In JavaScript
/how-to/send-graphql-with-curl-in-javascript
In Node.js
/how-to/send-graphql-with-curl-in-node
In Python
/how-to/send-graphql-with-curl-in-python
In Go
/how-to/send-graphql-with-curl-in-go
In PHP
/how-to/send-graphql-with-curl-in-php
In Ruby
/how-to/send-graphql-with-curl-in-ruby
In Java
/how-to/send-graphql-with-curl-in-java
In Kotlin
/how-to/send-graphql-with-curl-in-kotlin
Full Rust reference on the cURL to Rust hub, or open the main converter for all 18 targets.