Expand description
§Tutorial: Getting Started with Clawspec
Welcome to the Clawspec tutorial! This guide will walk you through using Clawspec to generate OpenAPI specifications from your test code.
§Learning Path
This tutorial is organized into chapters that build upon each other:
- Introduction - What is Clawspec and why use it
- Getting Started - Setting up the client and making your first request
- Request Building - POST requests, path and query parameters
- Response Handling - Processing responses and error handling
- Advanced Parameters - Headers, cookies, and parameter styles
- OpenAPI Customization - Tags, descriptions, and metadata
- Redaction - Stable examples with dynamic value redaction
- Test Integration - Using TestClient for end-to-end testing
§Quick Example
Here’s a taste of what you’ll learn:
use clawspec_core::ApiClient;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
#[derive(Serialize, ToSchema)]
struct CreateUser { name: String }
#[derive(Deserialize, ToSchema)]
struct User { id: u64, name: String }
// Create a client
let mut client = ApiClient::builder()
.with_host("api.example.com")
.build()?;
// Make a request - schema is captured automatically
let user: User = client
.post("/users")?
.json(&CreateUser { name: "Alice".to_string() })?
.await?
.as_json()
.await?;
// Generate OpenAPI specification
let spec = client.collected_openapi().await;
println!("{}", spec.to_pretty_json()?);Ready to start? Head to Chapter 0: Introduction!