Migrating from Hurl¶
Hurl and apic are close cousins: one binary each, plain-text files,
captures and assertions. The move is mostly mechanical. A Hurl entry's
sections become # @ directives above an .http request, and the files
become ones VS Code, JetBrains and Neovim can send with a click. Two
things change for the better on the way: captures persist between runs,
and a single named request can be run on its own.
How the concepts map¶
| Hurl | apic |
|---|---|
A .hurl file |
a .http file; apic run file.http runs it top to bottom as a flow |
| An entry (request plus response section) | a ### block with # @name, so it can also run alone |
HTTP 200 |
# @assert status == 200 |
[Captures] token: jsonpath "$.token" |
# @capture token = body.$.token |
header "Location", cookie "sid" |
header.location, cookie.sid |
jsonpath "$.items[0].id" |
body.$.items[0].id (filters, slices and .. too; see body paths) |
status, duration, body |
status, duration, body |
[Asserts] jsonpath "$.id" == 42 |
# @assert body.$.id == 42 |
==, !=, >, >=, <, <= |
the same |
contains, startsWith, endsWith, matches |
the same names |
exists, not exists |
the same |
isString, isInteger, isBoolean, isCollection, isEmpty |
the same, with isArray or isObject for isCollection |
jsonpath "$.items" count == 3 |
# @assert body.$.items length == 3 |
[Options] retry: 10 retry-interval: 2000 |
# @retry 10 2s |
[Options] delay: 500 |
# @sleep 500ms |
[Options] http2: true |
GET https://… HTTP/2 on the request line |
[Options] insecure: true, cacert, cert, key |
--insecure, or tls: in apic.yaml and --cacert, --cert, --key |
[BasicAuth] |
# @auth basic user pass |
[FormParams], [MultipartFormData] |
a urlencoded body, or a multipart body |
{{name}} with --variable name=value |
{{name}} with --var name=value |
--variables-file vars.env |
an environment in http-client.env.json, or .env |
HURL_name environment variables |
APIC_VAR_name |
--test, --glob |
apic run a.http b.http --keep-going |
--report-junit, --report-html, --report-json |
apic test --format junit for features; apic run --report report.html; apic run --json |
--json (one line per file) |
apic run --json (one object per request) |
A worked example¶
A Hurl file against the demo API:
POST http://localhost:8089/auth/login
{"user": "alice", "password": "{{password}}"}
HTTP 200
[Captures]
token: jsonpath "$.access_token"
GET http://localhost:8089/todos?done=false
Authorization: Bearer {{token}}
HTTP 200
[Asserts]
header "X-Total-Count" exists
jsonpath "$" count >= 1
jsonpath "$[0].done" == false
jsonpath "$[0].id" isString
The same two requests in apic (with apic demo running in another
terminal). The host moves into the environment, each entry gets a name,
and each section line becomes a directive:
mkdir -p from-hurl
cat > from-hurl/http-client.env.json <<'EOF'
{
"local": { "baseUrl": "http://localhost:8089" }
}
EOF
cat > from-hurl/todos.http <<'EOF'
### Log in
# @name login
# @assert status == 200
# @capture token = body.$.access_token
POST {{baseUrl}}/auth/login
Content-Type: application/json
{"user": "alice", "password": "{{password}}"}
### The open todos
# @name open-todos
# @assert status == 200
# @assert header.x-total-count exists
# @assert body.$ length >= 1
# @assert body.$[0].done == false
# @assert body.$[0].id isString
GET {{baseUrl}}/todos?done=false
Authorization: Bearer {{token}}
EOF
apic run todos.http -C from-hurl --env local --var password=s3cret | tail -4
✓ login 200 1 ms
✓ open-todos 200 0 ms
2 passed · 2 requests · 1 ms
Hurl adds Content-Type: application/json to a JSON body by itself; in a
.http file, write the header, since editors send exactly what the file
says. Then, because the token was captured into the session, the second
request runs on its own, which a Hurl file cannot do:
apic run open-todos -C from-hurl --env local | tail -1
What does not carry over¶
- XPath,
sha256,md5and the filter chain (jsonpath "$.x" split "," nth 1). apic's selectors are JSONPath, headers, cookies, status, duration and the raw body; a check that needs a transformation goes in a shell script aroundapic run --json. [Options]for curl internals such ascompressed,path-as-isorunix-socket. apic is not built on libcurl.--testreports per file. apic reports per request;--keep-goingand the summary line give the same pass/fail picture.- Hurl's own format. apic reads
.httponly; the two formats are close enough that a search-and-replace does most of it.
Checklist¶
- One
.httpfile per.hurlfile, a###block and# @nameper entry -
HTTP <code>to# @assert status == <code> -
[Captures]to# @capture,[Asserts]to# @assert,jsonpath "$…"tobody.$… - Hosts to
{{baseUrl}}inhttp-client.env.json;--variables-fileto an environment -
[Options] retryto# @retry - A
Content-Typeheader on each JSON body -
apic validateis clean andapic fmt --checkpasses -
hurl --testin CI replaced withapic run *.http --keep-going
See also: the assertion operators,
# @retry and
apic compared with Hurl.