x-http
An instant HTTP API testing suite - type-safe alternative to Postman/curl that integrates seamlessly with your Rust codebase.
Features
- Type-Safe Request Building: Compile-time safety for your API tests
- Chainable Assertions: Fluent API for validating responses
- JSON Path Queries: Easy navigation of nested JSON structures
- Works with
cargo test: Integrate API tests into your test suite - Interactive CLI Mode: Quick ad-hoc testing with prompts
- Configuration Files: Store and version control your requests
- Syntax Highlighting: Beautiful colored output for JSON responses
- Zero External Dependencies: No need for separate GUI applications
Installation
Or add to your Cargo.toml:
[]
= "0.1"
Quick Start
As a Library (Recommended)
use *;
As a CLI Tool
Interactive Mode
# Start interactive session
# Or explicitly
You'll be prompted for:
- HTTP method (GET, POST, PUT, etc.)
- URL
- Headers (optional)
- Request body (optional)
Quick Request
# Simple GET request
# POST with JSON body
# With headers
Configuration File
Create x-http.toml:
[]
= "https://api.example.com"
= "your-token-here"
[[]]
= "get-users"
= "GET"
= "{{BASE_URL}}/users"
[]
= "Bearer {{API_TOKEN}}"
[[]]
= "create-user"
= "POST"
= "{{BASE_URL}}/users"
= true
= """
{
"name": "John Doe",
"email": "john@example.com"
}
"""
[]
= "Bearer {{API_TOKEN}}"
= "application/json"
Run requests:
# Run all requests
# Run specific request
# Use custom config file
Usage Examples
Making Requests
use *;
// GET request
let response = get
.send
.unwrap;
// POST with JSON
let body = json!;
let response = post
.json
.unwrap
.send
.unwrap;
// PUT with text body
let response = put
.text
.send
.unwrap;
// DELETE request
let response = delete
.send
.unwrap;
// PATCH request
let response = patch
.json
.unwrap
.send
.unwrap;
Adding Headers
let response = get
.header
.header
.header
.send
.unwrap;
// Or add multiple at once
let headers = vec!;
let response = get
.headers
.send
.unwrap;
Query Parameters
let response = get
.query
.query
.query
.send
.unwrap;
// Results in: https://api.example.com/search?q=rust+programming&limit=10&offset=0
Timeouts and Redirects
use Duration;
let response = get
.timeout
.send
.unwrap;
// Disable automatic redirects
let response = get
.follow_redirects
.send
.unwrap;
// No timeout
let response = get
.no_timeout
.send
.unwrap;
Status Code Assertions
// Expect specific status
let response = get
.send
.unwrap
.expect_status
.unwrap;
// Expect any success status (2xx)
let response = post
.json
.unwrap
.send
.unwrap
.expect_success
.unwrap;
// Expect error status (4xx or 5xx)
let response = get
.send
.unwrap
.expect_error
.unwrap;
JSON Assertions
let response = get
.send
.unwrap
.expect_json // Validates Content-Type and parseable JSON
.unwrap
.assert_field
.unwrap
.assert_field
.unwrap
.assert_field
.unwrap
.assert_field_exists
.unwrap;
// Nested JSON paths
let response = get
.send
.unwrap
.assert_field
.unwrap
.assert_field
.unwrap;
// Array access
let response = get
.send
.unwrap
.assert_field
.unwrap
.assert_field
.unwrap
.assert_array_length
.unwrap;
Header Assertions
let response = get
.send
.unwrap
.expect_header
.unwrap
.expect_header
.unwrap;
// Shorthand for content-type
let response = get
.send
.unwrap
.expect_content_type
.unwrap;
Body Content Assertions
let response = get
.send
.unwrap
.expect_text // Validates body is valid UTF-8
.unwrap
.expect_body_contains
.unwrap
.expect_body_contains
.unwrap;
Extracting Response Data
// Get status
let status = response.status; // Returns u16
let status_code = response.status_code; // Returns StatusCode
// Check status type
if response.is_success
if response.is_error
// Get headers
let content_type = response.header;
let all_headers = response.headers;
// Get body
let text = response.text.unwrap;
let bytes = response.body_bytes;
// Parse JSON
let user: User = response.json.unwrap;
// Or as generic JSON value
let json: Value = response.json_value.unwrap;
// Get request duration
let duration = response.duration;
println!;
Why x-http?
vs Postman
- ✅ Version controlled (requests live in code)
- ✅ Works in CI/CD without GUI
- ✅ Type-safe with compile-time checks
- ✅ Integrates with
cargo test - ✅ No account/login required
- ✅ Free and open source
vs curl
- ✅ Type-safe assertions
- ✅ Better error messages
- ✅ JSON path queries
- ✅ Chainable API
- ✅ Syntax highlighting
- ✅ Interactive mode
vs other Rust HTTP clients
- ✅ Built for testing, not production
- ✅ Assertion-focused API
- ✅ CLI included
- ✅ Configuration file support
- ✅ Interactive mode
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Licensed under either of:
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.