HTTPie
NotesHTTPie (pronounced aitch-tee-tee-pie) is a command line HTTP client, like curl, but designed to be friendly: short commands, JSON by default, and coloured, formatted output. It’s handy for trying out the APIs in this guide.
Install it on Fedora with sudo dnf install httpie, or with pip install httpie in your virtual environment. The command is http.
The examples use the application running with fastapi dev main.py, and CONF_ENABLE_APIS=true in .env. :8000 is HTTPie’s shorthand for http://localhost:8000.
Requests#
Check the health API, with --print=hb to show the response headers as well as the body:
http --print=hb :8000/api/v1/healthHTTP/1.1 200 OK
content-length: 15
content-type: application/json
server: uvicorn
x-health-check: ok
x-process-time: 0.00036067399923922494
{
"health": "ok"
}Request items after the URL become the request. name=value is a JSON string, and name:=value raw JSON, such as a number or true. With any of them, HTTPie sends a POST:
http POST :8000/api/v1/blog/create title="Hello HTTPie" teaser="From the command line" \
content="Some *Markdown*" cover_image=/static/img/post-bg.jpg published:=trueQuery parameters go in the URL as usual (quote it, because of the ?), or as name==value:
http :8000/api/v1/blog limit==1Forms and logging in#
--form (or -f) sends a form instead of JSON, which is what the token API expects:
http --form POST :8000/api/v1/auth/token username=ada@example.com password=password123{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "bearer"
}For the website’s pages, which use the login cookie, a session remembers cookies between commands:
http --session=ada --form POST :8000/auth/login username=ada@example.com password=password123http --session=ada :8000/profileThe first command logs in, and HTTPie saves the access_token cookie in the session called ada; the second sends it back, so the profile page loads as Ada.
In scripts, where nothing is typed in, HTTPie reads its standard input as the request body. Add --ignore-stdin to stop that, or HTTPie complains that it can’t mix the body with name=value items.