Home
🦀
cURL to Rust reqwest

cURL to Rust reqwest Code Generator

Generate idiomatic, production-ready Rust reqwest client code from any cURL command. Supports Tokio async/await, Serde JSON typed struct inference, multipart file uploads, and complete Cargo.toml configurations.

Tokio v1.x Serde Derive Client-Side Only
Rust Generator Settings
TLS:
Input cURL Command
POSThttps://api.openai.com/v1/chat/completionsBearer AuthBody: json1 Headers
Reqwest Performance Advice

In high-throughput microservices, always instantiate reqwest::Client once and reuse it (e.g. inside an Arc<Client> or application state). Reqwest maintains an internal HTTP keep-alive connection pool across requests.

use reqwest::Client;
use serde::{Deserialize, Serialize};

// Strongly-typed Request payload generated from cURL body
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MessagItem {
    pub role: String,
    pub content: String,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RequestPayload {
    pub model: String,
    pub temperature: f64,
    pub max_tokens: i64,
    pub stream: bool,
    pub messages: Vec<MessagItem>,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Reusable reqwest client (thread-safe, internal Arc connection pool)
    let client = Client::new();

    // Construct type-safe request payload
    let payload = RequestPayload {
        model: "gpt-4o".to_string(),
        temperature: 0.7,
        max_tokens: 1024,
        stream: false,
        messages: vec![
            MessagItem {
                role: "system".to_string(),
                content: "You are an expert Rust systems programmer.".to_string(),
            },
            MessagItem {
                role: "user".to_string(),
                content: "Explain zero-cost abstractions in reqwest and tokio.".to_string(),
            }
        ],
    };

    // Build and dispatch HTTP request
    let response = client.post("https://api.openai.com/v1/chat/completions")
        .bearer_auth("sk-proj-1234567890abcdef")
        .json(&payload)
        .send()
        .await?;

    // Inspect HTTP status code
    let status = response.status();
    println!("Status Code: {}", status);

    // Parse dynamic JSON payload into generic serde_json::Value
    if status.is_success() {
        let json_body: serde_json::Value = response.json().await?;
        println!("JSON Response:\n{}", serde_json::to_string_pretty(&json_body)?);
    } else {
        let err_body = response.text().await?;
        eprintln!("API returned error {}: {}", status, err_body);
    }

    Ok(())
}
Serde Struct Inference

Automatically parses JSON bodies in cURL commands and creates strongly-typed Rust structs with proper field naming, snake_case mapping, and serde renames.

Tokio & Sync Modes

Switch effortlessly between modern async/await powered by Tokio runtime and straightforward blocking reqwest for quick CLI utilities and scripts.

100% In-Browser Privacy

Every token, header, and payload is parsed inside your client web browser. No API keys or sensitive authorization tokens are ever transmitted to any server.

Frequently Asked Questions & Technical Notes

How does this tool handle multi-line cURL commands?

The tokenizer cleans POSIX backslashes (\), Windows cmd carats (^), and PowerShell backticks (`), preserving whitespace and string quotes accurately.

Why does reqwest require Tokio runtime by default?

reqwest is built on top of hyper, a fast asynchronous HTTP engine. Tokio provides the asynchronous I/O polling loop and task scheduler.

Can I convert multipart file upload forms?

Yes! When cURL flags like -F "file=@/path" are detected, the generator includes the "multipart" feature in Cargo.toml and constructs a reqwest::multipart::Form.

Is my authentication token safe?

Absolutely. This utility runs completely client-side in your browser ('use client'). There are zero network calls or telemetry endpoints collecting your credentials.