curl-parser
A Rust library for parsing curl commands into structured HTTP request objects.
Overview
Many APIs provide curl examples to help users get started quickly. This crate bridges the gap between curl command examples and Rust code by parsing curl commands into structured ParsedRequest
objects that can be easily converted to HTTP requests.
Features
- Parse curl commands into structured Rust objects
- Template support for dynamic values (e.g., API tokens)
- Automatic conversions for common patterns
- reqwest integration (optional)
- High performance with optimized parsing
Supported curl Options
-X, --request
- HTTP method-H, --header
- HTTP headers-d, --data
- Request body-u
- Basic authentication-L, --location
- Follow redirects-k, --insecure
- Skip SSL verification
Installation
Add this to your Cargo.toml
:
[]
= "0.6"
Feature Flags
reqwest
(enabled by default) - Enables conversion toreqwest::RequestBuilder
uri
(enabled by default) - Parses URLs intohttp::Uri
type
To use without default features:
[]
= { = "0.6", = false }
Quick Start
Basic Usage
use ParsedRequest;
use FromStr;
let curl_cmd = "curl https://api.example.com/users";
let request = from_str?;
println!;
println!;
With Template Variables
use ParsedRequest;
use json;
let curl_cmd = r#"curl -X POST https://api.github.com/repos \
-H "Authorization: Bearer {{ token }}" \
-d '{"name": "{{ repo_name }}"}"#;
let context = json!;
let request = load?;
Convert to reqwest
use ParsedRequest;
use FromStr;
async
Examples
POST with JSON Body
let curl = r#"curl -X POST https://api.example.com/users \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer token123' \
-d '{"name": "John Doe", "email": "john@example.com"}"#;
let request = from_str?;
assert_eq!;
assert_eq!;
Basic Authentication
let curl = r#"curl https://api.stripe.com/v1/charges \
-u sk_test_1234: \
-H "Stripe-Version: 2022-11-15""#;
let request = from_str?;
// The -u flag is automatically converted to Authorization: Basic header
Form Data
let curl = r#"curl -X POST https://httpbin.org/post \
-d 'name=John' \
-d 'age=30' \
-d 'city=New York'"#;
let request = from_str?;
// Multiple -d flags are collected and form-urlencoded
Advanced Features
Escaped JSON in Headers
The parser correctly handles escaped JSON in headers:
let curl = r#"curl https://api.example.com \
-H "X-Custom-Data: {\"key\":\"value\",\"nested\":{\"data\":true}}"#;
let request = from_str?;
// The escaped JSON is properly unescaped in the header value
Automatic Method Inference
If a request body is provided without an explicit method, POST is automatically used:
let curl = r#"curl https://api.example.com -d '{"data": "value"}'"#;
let request = from_str?;
assert_eq!; // Automatically set to POST
Default Headers
The parser automatically adds common default headers:
Accept: */*
if not specifiedContent-Type: application/x-www-form-urlencoded
for form data
Performance
This crate is optimized for performance with:
- Cached template environment (60%+ improvement for template operations)
- Pre-allocated collections for common sizes
- Efficient string operations using byte-level matching
- Optimized grammar rules for the Pest parser
Run benchmarks with:
Development
Building
Testing
Linting
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.