Migrating from curl and Taskfile
This is the move apic was made for: requests that live as curl commands in
a Taskfile, a Makefile, a wiki page or shell history, with the token
pasted in by hand. Each command becomes a named request in a .http file
that editors can send, agents can read and CI can run, and Task, if you
use it, stays as the front door.
How the concepts map
Section titled “How the concepts map”| curl and a Taskfile | apic |
|---|---|
| A curl command | a ### block with # @name in a .http file |
-X POST, -H, -d, -F, -u | the request line, headers, body, a multipart body, # @auth basic |
$BASE_URL, $TOKEN in the shell | {{baseUrl}} from http-client.env.json, {{token}} captured by a login request |
TOKEN=$(curl … | jq -r .token) | # @capture token = body.$.access_token, kept in the session between commands |
curl -f, | grep, | jq -e to check a result | # @assert status == 200, # @assert body.$.id exists |
-k, --cacert, --cert, -x | --insecure, --cacert, --cert, --proxy, or tls: and proxy: in apic.yaml |
| A task per request | task api -- <name>, one passthrough task (see Taskfile) |
task --list | apic list, with methods, URLs and descriptions |
| Copy the command from the wiki | apic curl <name> prints it back, runnable as printed; --redact for one that is safe to paste into a log |
Turn each command into a request
Section titled “Turn each command into a request”apic import --curl reads a curl command, in either quote style and
with backslash continuations, and appends a request to a file. With
apic demo running in another terminal:
mkdir -p from-curlcat > from-curl/http-client.env.json <<'EOF'{ "local": { "baseUrl": "http://localhost:8089" }}EOFprintf 'env: local\n' > from-curl/apic.yamlapic import -C from-curl --into auth.http --name login \ --curl 'curl -s -X POST http://localhost:8089/auth/login -H "Content-Type: application/json" -d '"'"'{"user":"alice","password":"s3cret"}'"'"''apic import -C from-curl --into todos.http --name list-todos \ --curl 'curl -s http://localhost:8089/todos -H "Authorization: Bearer mock-token"'cat from-curl/auth.http from-curl/todos.httpadded login to auth.httpadded list-todos to todos.http### POST /auth/login# @name login# @assert status == 200POST {{baseUrl}}/auth/loginContent-Type: application/json
{"user":"alice","password":"s3cret"}### GET /todos# @name list-todos# @assert status == 200GET {{baseUrl}}/todosAuthorization: Bearer mock-tokenThe host became {{baseUrl}} because the environment’s baseUrl
prefixes it, and each request got a status check. What the importer
cannot know is where the secrets come from: the password and the token
are still pasted in.
Take the secrets out
Section titled “Take the secrets out”Move the password to the private env file, which stays out of git,
capture the token from the login response, and let list-todos log in by
itself when it has none:
cat > from-curl/http-client.private.env.json <<'EOF'{ "local": { "password": "s3cret" }}EOFcat > from-curl/auth.http <<'EOF'### Log in and keep the token# @name login# @assert status == 200# @capture token = body.$.access_tokenPOST {{baseUrl}}/auth/loginContent-Type: application/json
{"user": "alice", "password": "{{password}}"}EOFcat > from-curl/todos.http <<'EOF'### List todos# @name list-todos# @ref login# @assert status == 200GET {{baseUrl}}/todosAuthorization: Bearer {{token}}EOFapic session clear -C from-curlapic run list-todos -C from-curl | grep -E '^(POST|GET|✓|↳)'POST http://localhost:8089/auth/login✓ status == 200↳ token = mock-token↳ ran login first (# @ref)GET http://localhost:8089/todos✓ status == 200Keep Task as the front door
Section titled “Keep Task as the front door”A Taskfile of curl commands:
version: "3"tasks: login: cmds: - curl -s -X POST "$BASE_URL/auth/login" -H "Content-Type: application/json" -d "{\"user\":\"alice\",\"password\":\"$PASSWORD\"}" | jq -r .access_token > .token todos: cmds: - curl -sf "$BASE_URL/todos" -H "Authorization: Bearer $(cat .token)"becomes one task that passes its arguments through, plus named tasks for the ones people type every day:
version: "3"tasks: api: desc: Run an API request, e.g. task api -- list-todos --env staging dir: from-curl cmds: - apic run {{.CLI_ARGS}} todos: desc: List the todos dir: from-curl cmds: - apic run list-todosThe .token file, the jq and the -f are gone: the session holds the
token, # @ref fetches it, and apic run exits non-zero when an
assertion fails (1), a request cannot be built (2) or the network fails
(3). More patterns are in Using apic with Taskfile.
And back to curl
Section titled “And back to curl”For the machine with no apic, or a bug report, apic curl prints the
command again with every variable filled in, so it runs as printed. With
--redact, header, body and query values are masked (a # @auth
credential becomes a shell placeholder such as $TOKEN), for pasting into
a ticket or a log:
apic curl list-todos -C from-curlapic curl list-todos -C from-curl --redactcurl -sS \ -H 'Authorization: Bearer mock-token' \ 'http://localhost:8089/todos'curl -sS \ -H 'Authorization: ***' \ 'http://localhost:8089/todos'apic snippet list-todos --lang python does the same for HTTPie,
PowerShell, Python, JavaScript and Go.
What does not carry over
Section titled “What does not carry over”- Shell logic between commands. Loops, conditionals and computed
values stay in the shell, around
apic run --json, which prints one JSON object per request forjq. - curl’s own output flags (
-o,-w,-v,--trace). apic has-v,--json,--body-onlyand--output <file>; the importer notes the flags that change behaviour, such as-o,-wand--trace, and drops-s,-v,-iand-fsilently. - Protocols other than HTTP. apic does not do FTP, SMTP or the rest of what curl speaks.
Checklist
Section titled “Checklist”-
apic import --curl '…' --into <file>.http --name <name>for each command - Hosts to
{{baseUrl}}inhttp-client.env.json, one environment per target - Passwords and keys to
http-client.private.env.json;.apic/and the private file in.gitignore - Hand-pasted tokens replaced with
# @captureon the login and# @ref loginwhere it is needed - Checks done with
greporjq -emoved to# @assert - Taskfile tasks reduced to
apic run <name> -
apic validateis clean andapic fmt --checkpasses
See also: apic import --curl,
apic curl, apic snippet
and Using apic with Taskfile.
Hosted on Cloudflare, which keeps aggregate traffic statistics. No cookies, no tracking scripts, no per-request logs. Privacy