std/http

Requests go through the curl program, which Windows 10 and later, macOS, and most Linux systems include. Without it every request is an ERROR that says so.

Function Result
http_get(url) a Result[HttpResponse]
http_post(url, body, content_type) the same, sending body
http_request(method, url, headers, body) any method, with headers from a map[str, str]
http_download(url, path) saves the body to a file, text or not, and gives the status
http_ok(response) whether the status is from 200 to 299
url_encode(text) text with everything but letters, digits, and - _ . ~ written as %XX, from std/encoding, which std/http passes on

An HttpResponse has the status, such as 200 or 404, the body as text, and the headers, with names in lower case. A response with any status is OK; an ERROR means there was no response, as when the server cannot be reached, and carries curl's message. Redirects are followed.

import "std/http"
import "std/json"

str url = "https://api.github.com/search/repositories?q=" + url_encode("language:c stars:>1000")
match http_get(url) do
    when OK(response) then
        match parse_json(response.body) do
            when OK(data) then
                print(json_number(json_get(data, "total_count"), 0.0))
            when ERROR(message) then
                eprint(message)
        end
    when ERROR(message) then
        eprint(message)
end