Skip to content

web

import web
import web
fn main {
match web.get("https://example.com/") {
ok(page) => {
print(page.status)
print(page.header("Content-Type"))
print(page.body.length)
}
err(e) => print("no answer: {e}")
}
}
FunctionGives
web.get(url) -> web.Response or web.WebErrorasks for a page.
web.post(url) body(text) -> web.Response or web.WebErrorsends a text, a form or JSON.
web.send(request) -> web.Response or web.WebErrorsends a request built step by step (below).

An answer is always ok, even a 404 or a 500: the site did answer, and page.status says what. err means there was no answer at all. This keeps the two questions apart: “did the network work?” is the match, “did the site agree?” is the status.

Redirections are followed, 10 at most; page.url says where it ended.

Field or methodHolds
page.status -> Intthe status: 200, 404…
page.body -> Stringthe text of the answer
page.bytes -> List<Int>the same answer as bytes, for an image or an archive: body turns what is not text into �
page.url -> Stringthe address that answered, after redirections
page.headers -> Map<String, String>every header, names in small letters
page.header(name) -> String?one header, whatever the capitals of name

With JSON, the body becomes a struct of your own in one line: Book.from_json(page.body) (JSON).

For another method, headers, or a time limit, build a web.Request, then send it:

import web
fn main {
let asked: web.Request := web.Request.put("https://example.com/notes/1")
.header("Accept") is("text/plain")
.body("hello")
.seconds(5)
.no_redirections
match web.send(asked) {
ok(page) => print(page.status)
err(e) => print("no answer: {e}")
}
}
WrittenGives
web.Request.get(url), .post, .put, .patch, .delete, .heada request with that method
r.header(name) is(value)the request with this header
r.body(text)the request with this body
r.bytes(data: List<Int>)the request with these bytes as its body, sent instead of the text if any
r.seconds(s: Float)the most to wait for each step; 0 waits forever
r.no_redirectionsa redirection is given back as it is, instead of followed

Each step gives a changed copy, so a base request can be kept and reused. Without seconds, Kova waits 30 seconds at most for each step (connecting, each piece of the answer).

Each case holds the address asked:

CaseMeans
BadUrl(url)the address is not one Kova can ask (http:// or https://)
NoHost(url)the name does not exist, or there is no network
Refused(url)the computer is there, but nothing answers on that port
Timeout(url)no answer in time
Broken(url)the connection broke in the middle
BadAnswer(url)what came back is not HTTP
TooManyRedirects(url)more than 10 redirections
BadCertificate(url)the site’s certificate is not valid for it
NoHttps(url)no TLS library on this Linux computer
BadHeader(url)a header Kova writes itself (Host, Content-Length…), or one holding a line break

https:// uses each system’s own encryption (SChannel on Windows, Network.framework on macOS, OpenSSL on Linux), so security fixes come with the system’s updates. Certificates are always checked; there is no switch to turn that off.

On Linux, a program using web needs the system’s C library, and https:// needs OpenSSL, found when the program runs. On Windows and macOS, nothing else is needed.

To answer questions instead of asking them, see the web server.