Http.get
func Http.get(url: string, headers: Map[string, string] = Map{}, timeout: Time.Duration | null = null, ctx: Context | null = null): Result[Http.Response, Error]Http.get sends a GET request to url and reads the whole response. It is Http.request with the method "GET" and an empty body.
Details
- Every response is
Ok, whatever its status: a 404 or a 500 isOkwith thatstatus.Errmeans no response arrived: a malformed URL, no connection, header names that differ only in case, the timeout, or the context. - No redirect is followed. A 3xx comes back as it is, with its
Locationheader. - The whole body is read before
Http.getreturns, and it is held once, as received, inbytes. Http.text decodes it, and bytes that are not valid UTF-8 become U+FFFD. headersholds one string per name. A header the server repeats is joined with,.timeoutbounds the whole call, from connecting to the end of the body. A timeout that is zero or negative, or that has years, months, or weeks, isErrbefore anything is sent. Withouttimeoutorctx, the call has no deadline and waits as long as the server takes.
Basic examples
Start a server on a free local port, and get a page from it:
let listener = try Net.listen("127.0.0.1:0")
go { _ = Http.serve(listener, { Http.Reply{ status: 200, headers: Map{}, body: "Hello, ${x.path}" } }) }
let response = try Http.get("http://${Net.address(listener)}/world")
println(response.status)
println(response.text())200
Hello, /worldThe examples below run against a local server like this one, at the address base. Each says what the paths it asks for answer. Here /hello answers Hello, Ant, and a path the server does not know answers 404:
println((try Http.get("${base}/hello")).text())
let missing = try Http.get("${base}/nothing")
println("${missing.status}: ${missing.text()}")Hello, Ant
404: not found: /nothingWhen no server answers, the result is Err, and the message names the URL:
println(Http.get("http://127.0.0.1:1/"))Err{ error: Error{ message: "get \"http://127.0.0.1:1/\": connection refused" } }Scope
A query string
The query goes in the URL as written. Escape each value with Url.escape. Here /search answers with the raw query it received:
let term = "red & blue"
let response = try Http.get("${base}/search?q=${Url.escape(term)}&page=2")
println(response.text())query: q=red+%26+blue&page=2A redirect
Http.get returns the redirect itself. Here /old answers 301 with Location: /hello:
let response = try Http.get("${base}/old")
println(response.status)
println(response.headers["Location"])301
/helloAn empty body
A response without a body has no bytes in bytes, and its text() is "". Here /empty answers 204:
let response = try Http.get("${base}/empty")
println(response.status)
println(response.text() == "")
println(response.bytes.length())204
true
0A binary body
Bytes that are not UTF-8 become U+FFFD in text(), while bytes keeps them as received. Here /logo answers the four bytes a PNG file starts with:
let response = try Http.get("${base}/logo")
println(response.bytes)
println(response.text().count("\u{FFFD}"))Bytes(89 50 4e 47)
1A malformed URL
A URL without a scheme is Err before anything is sent:
println(Http.get("example.com/index.html"))Err{ error: Error{ message: "get \"example.com/index.html\": unsupported protocol scheme \"\"" } }Named arguments
headers
headers are sent with the request. Here /accept answers with the Accept header it received:
println((try Http.get("${base}/accept", headers: Map{ "Accept": "application/json" })).text())
println((try Http.get("${base}/accept")).text())application/json
nonetimeout
timeout bounds the whole call. When it passes, the result is Err with the message timeout. Here /slow answers after one second:
println(Http.get("${base}/slow", timeout: 100ms))
println(Http.get("${base}/slow", timeout: 5s).map({ x.text() }))Err{ error: Error{ message: "timeout" } }
Ok{ value: "done" }ctx
A context bounds the call as well, and shares the bound with other work. When the context is done, the result is Err with the context's reason:
let ctx = Context.new()
go {
Time.sleep(100ms)
Context.cancel(ctx)
}
println(Http.get("${base}/slow", ctx: ctx))Err{ error: Error{ message: "cancelled" } }When both are given, the earlier bound wins:
let ctx = Context.new(10s)
println(Http.get("${base}/slow", timeout: 100ms, ctx: ctx))Err{ error: Error{ message: "timeout" } }Applications
Fetch JSON
Check the status, then parse the body with Json.parse. Here /users/1 answers with a user, and /users/2 is not found:
func fetchJson(url: string): Result[Json.Value, Error] {
let response = try Http.get(url)
if response.status != 200 {
return Result.fail("get ${url}: status ${response.status}")
}
Json.parse(response.text()) | Result.wrapErr("parse ${url}")
}
let user = try fetchJson("${base}/users/1")
if user is Map { println(user["name"]) }
println(fetchJson("${base}/users/2").isOk())Ann
falseFollow redirects
Ask for the path in each redirect's Location, up to a limit. Here /old redirects to /hello, and /loop redirects to itself:
func getFollowing(path: string, limit: int): Result[Http.Response, Error] {
var next = path
for _ in Iter.range(0, limit) {
let response = try Http.get("${base}${next}")
let location = response.headers["Location"]
if response.status < 300 || response.status > 399 || location == null {
return Result.ok(response)
}
next = location
}
Result.fail("get ${path}: more than ${limit} redirects")
}
println((try getFollowing("/old", 5)).text())
println(getFollowing("/loop", 5))Hello, Ant
Err{ error: Error{ message: "get /loop: more than 5 redirects" } }Fetch several pages at once
Parallel.map runs the requests on goroutines and keeps the order of the URLs:
let paths = ["/hello", "/users/1", "/nothing"]
let results = Parallel.map(
paths,
func(path: string, work: Context) { Http.get("${base}${path}", ctx: work) },
limit: 3,
)
for result in results ?? [] {
println(result.map({ x.status }))
}Ok{ value: 200 }
Ok{ value: 200 }
Ok{ value: 404 }Properties and relations
Http.get(url) is Http.request with the method "GET":
let viaGet = try Http.get("${base}/hello")
let viaRequest = try Http.request("GET", "${base}/hello")
assert(viaGet.status == viaRequest.status && viaGet.text() == viaRequest.text())text() is bytes decoded, with U+FFFD for bytes that are not UTF-8:
for path in ["/hello", "/logo"] {
let response = try Http.get("${base}${path}")
assert(response.text() == String.fromBytesLossy(response.bytes))
}Possible issues
A 404 is not an Err
try passes on every response, so a program that skips the status check reads the error page as if it were the one it asked for. Here /users/2 answers 404 with the text not found: /users/2, and parsing it as JSON fails on that text, with a message that says nothing of the 404:
let response = try Http.get("${base}/users/2")
println(Json.parse(response.text()))Err{ error: Error{ message: "expected \"null\", found 'o' at line 1, column 2" } }Check status before reading the body, as Fetch JSON does.
A timeout does not name the URL
A failed connection names the URL in its message, but a timeout's message is only timeout, and a context's reason names no request either. Wrap the error with Result.wrapErr to say which request failed. Here /slow answers after one second, and the wrap names its path:
println(Http.get("${base}/slow", timeout: 100ms) | Result.wrapErr("get /slow"))Err{ error: Error{ message: "get /slow: timeout" } }A timeout in weeks
A week, a month, or a year has no fixed length, so a timeout with one is Err, as is one that is not positive:
println(Http.get("http://127.0.0.1:1/", timeout: Time.duration(weeks: 1)))
println(Http.get("http://127.0.0.1:1/", timeout: 0s))Err{ error: Error{ message: "get \"http://127.0.0.1:1/\": the timeout has years, months or weeks, which have no fixed length" } }
Err{ error: Error{ message: "get \"http://127.0.0.1:1/\": the timeout is zero or negative" } }Header names that differ only in case
Header names are not case sensitive, so two that differ only in case name one header, and the call is Err:
println(Http.get("http://127.0.0.1:1/", headers: Map{ "Accept": "text/html", "accept": "text/plain" }))Err{ error: Error{ message: "get \"http://127.0.0.1:1/\": the headers \"Accept\" and \"accept\" differ only in case" } }The result is a Result
The response is inside the Result, so reading a field of it directly is a compile error:
let status = Http.get("http://127.0.0.1:1/").status
// error: Result[Http.Response, Error] has no field status on every member: narrow the value firstSee also
Http.request · Http.post · Http.serve · Http.Response · Url.escape · Json.parse · Parallel.map · Context.new
