7.6.1. HV-01 — Simple HTTP Requests

This tutorial covers daslang’s HTTP client bindings in the dashv module — making GET, POST, PUT, PATCH, DELETE, and HEAD requests, passing custom headers, and reading responses.

7.6.1.1. Setup

Import the dashv module (via its boost helper):

require dashv/dashv_boost

All request functions create a fresh connection per call — ideal for scripts, tools, and one-off requests. For persistent connections, custom timeouts, and authentication, see HV-02 — Advanced HTTP Requests.

7.6.1.2. GET Requests

The simplest call retrieves a resource with no body:

GET("http://example.com/api") <| $(resp) {
    print("status: {resp.status_code}, body: {resp.body}\n")
}

Pass custom headers with a table literal:

GET(url, {"Accept" => "text/plain", "X-Request-Id" => "42"}) <| $(resp) {
    print("{resp.status_code}: {resp.body}\n")
}

7.6.1.3. POST Requests

POST sends a body string. An optional headers table is accepted as the third argument:

POST(url, "hello") <| $(resp) {
    print("{resp.status_code}: {resp.body}\n")
}

// with headers
POST(url, "\{\"msg\":\"hi\"\}", {"Content-Type" => "application/json"}) <| $(resp) {
    print("{resp.status_code}: {resp.body}\n")
}

7.6.1.4. PUT and PATCH

PUT and PATCH follow the same signature as POST — a URL, a body, and optional headers and form-data tables:

PUT(url, "payload") <| $(resp) { ... }
PUT(url, "payload", headers) <| $(resp) { ... }
PUT(url, "", headers, form_data) <| $(resp) { ... }

PATCH(url, "payload") <| $(resp) { ... }
PATCH(url, "payload", headers) <| $(resp) { ... }
PATCH(url, "", headers, form_data) <| $(resp) { ... }

7.6.1.5. DELETE and HEAD

DELETE takes a URL and optional headers. HEAD is identical in signature to GET but returns only the response headers (no body):

DELETE(url) <| $(resp) { ... }
HEAD(url) <| $(resp) { ... }

7.6.1.6. Response Headers

Read individual headers with get_header, or iterate all with each_header. Both accept an HttpResponse? pointer. each_header includes Set-Cookie entries (on responses) and Cookie entries (on requests) — see Tutorial 05 for the parsed each_cookie alternative.

GET(url) <| $(resp) {
    let ct = get_header(resp, "Content-Type")
    print("Content-Type: {ct}\n")

    each_header(resp) <| $(key, value) {
        print("  {key}: {value}\n")
    }
}

7.6.1.7. Status Message

status_message returns the human-readable reason phrase (e.g. "OK", "Not Found"):

GET(url) <| $(resp) {
    let msg = status_message(resp)
    print("Status: {resp.status_code} {msg}\n")
}

7.6.1.8. Binary Data

resp.body is a string, but resp.content_length tells you the exact byte count for binary payloads:

GET(url) <| $(resp) {
    print("Received {resp.content_length} bytes\n")
}

7.6.1.9. Quick Reference

Function

Description

GET(url) <| $(resp) { ... }

GET request

GET(url, headers) <| ...

GET with custom headers

POST(url, body) <| ...

POST request

POST(url, body, headers) <| ...

POST with custom headers

PUT(url, body) <| ...

PUT request

PUT(url, body, headers) <| ...

PUT with custom headers

PATCH(url, body) <| ...

PATCH request

PATCH(url, body, headers) <| ...

PATCH with custom headers

DELETE(url) <| ...

DELETE request

DELETE(url, headers) <| ...

DELETE with custom headers

HEAD(url) <| ...

HEAD request

HEAD(url, headers) <| ...

HEAD with custom headers

get_header(resp, key)

Read a single response header

each_header(resp) <| $(k, v) {}

Iterate all headers (including Set-Cookie)

status_message(resp)

Human-readable status phrase