yttp - "Better HTTP"
yttp is a Rust library providing a JSON/YAML façade for HTTP requests and responses.
It offers header shortcuts, smart auth, content-type-driven body encoding, and structured response formatting.
Used by yurl — the CLI HTTP client built on yttp.
use ;
// Parse a "Better HTTP" request
let val = parse?;
let req = parse_request?;
// req.method == "GET", req.headers["Authorization"] == "Bearer my-token"
// Send with your preferred HTTP client, then format the response
let resp = Response ;
let output = format_response;
// output["s"]["c"] == 200, output["b"]["id"] == 1
Reference
Commented YAML schema by example.
Request (input)
# --- Method + URL ---
g: https://httpbin.org/get # shortcuts: g p d (get post delete)
# full: get post put delete patch head options trace
# any capitalization accepted
# --- Request (input) headers ---
h:
X-Request-Id: abc-123 # regular headers pass through as-is
# key shortcuts — Authorization
a!: my-token # → Authorization: Bearer my-token (bare token)
a!: # → Authorization: Basic base64(user:pass)
a!: Basic dXNlcjpwYXNz # → passthrough (string with space = has scheme)
a!: basic!user:pass # → explicit prefix form: Basic base64(user:pass)
a!: bearer!my-token # → explicit prefix form: Bearer my-token
# key shortcuts — Content-Type
c!: f! # → Content-Type: application/x-www-form-urlencoded
# value shortcuts — content types
Accept: j! # → application/json (also: json!)
Accept: f! # → application/x-www-form.. (also: form!)
Accept: m! # → multipart/form-data (also: multi!)
Accept: h! # → text/html (also: html!)
Accept: t! # → text/plain (also: text!)
Accept: x! # → application/xml (also: xml!)
# value shortcuts — prefixes
Accept: a!/json # → application/json
Accept: t!/csv # → text/csv
Accept: i!/png # → image/png
# --- Request (input) query params ---
q: # merged into URL query string
term: foo # ?term=foo&limit=10
limit: 10 # URL-encoded automatically
# arrays → repeated keys: tags: [a, b] → tags=a&tags=b
# appends to existing ?params if present
# --- Request (input) body ---
b: # encoding depends on Content-Type:
city: Berlin # application/json (default, c!: j! is implied)
lang: de # c!: f! → form-urlencoded
# c!: m! → multipart (file:// values read from disk)
Response (output)
Default structured format: {s, h, b}
# --- Response (output) status ---
s: # inline object (default via status_inline())
s: HTTP/1.1 200 OK # raw status line (via status.line)
# --- Response (output) headers ---
h:
content-type: application/json
server: gunicorn/19.9.0
# --- Response (output) body (via encode_body()) ---
# JSON response → preserved as structure:
b:
city: Berlin
lang: de
# UTF-8 text response → string (block scalar for multi-line):
b: |-
<!doctype html>
<html>
<body>Hello</body>
</html>
# binary response → base64 string:
b: "SGVsbG8gV29ybGQ=..."
API
| Function | Description |
|---|---|
parse(s) |
Parse JSON or YAML string into Value |
parse_request(val) |
Parse request value, expand header shortcuts, merge q: into URL → Request |
parse_url(url) |
Parse URL into components → UrlParts |
encode_body(bytes) |
Smart body encoding: JSON → value, UTF-8 → string, binary → base64 |
status_inline(status) |
Format status as {v, c, t} object |
format_response(resp) |
Full response as {s, h, b} value |
headers_to_raw(headers) |
Headers map to raw HTTP string |
expand_headers(headers) |
Expand shortcut keys and values in-place |
build_query_string(obj) |
Build URL query string from key-value object |
append_query_to_url(url, q) |
Append q: params to URL string (no-op if None/empty) |
All functions that can fail return yttp::Result<T>.