http.um

struct Response

Http response struct.

type Response* = struct {
ok: bool         // true if the request was successful
err: str         // error message if the request failed
status: int      // HTTP status code
contentType: str // response content type
body: []char     // body of the response
}

struct GetParams

Optional parameters for the get function.

type GetParams* = struct {
userAgent: str
headers: []str
}

fn get

Make a GET request.

fn get*(url: str, params: GetParams = {}): Response {

fn encodeUrl

Encode a URL with query parameters.

fn encodeUrl*(url: str, args: map[str]any): str {

struct PostParams

Optional parameters for the post function.

type PostParams* = struct {
userAgent: str
headers: []str
}

fn post

Make a POST request.

fn post*(url: str, body: []char, params: PostParams = {}): Response {

Example

fn main() {
resp := get(
encodeUrl("https://httpbin.org/get", {
"foo": "bar",
"baz": 42
}),
{
userAgent: "my user agent"
}
)

printf("Ok: %v\n", resp.ok)
printf("Error: %v\n", resp.err)
printf("Code: %v\n", resp.status)
printf("Content type: %v\n", resp.contentType)
printf("Body: %v\n", str(resp.body))

resp = post(
"https://httpbin.org/post",
[]char("Hello world"),
{
userAgent: "my user agent",
headers: { "Content-Type: text/plain" }
}
)

printf("Ok: %v\n", resp.ok)
printf("Error: %v\n", resp.err)
printf("Code: %v\n", resp.status)
printf("Content type: %v\n", resp.contentType)
printf("Body: %v\n", str(resp.body))
}


import (
    "std.um"
)

//~~struct Response
// Http response struct.
type Response* = struct {
    ok: bool         // true if the request was successful
    err: str         // error message if the request failed
    status: int      // HTTP status code
    contentType: str // response content type
		body: []char     // body of the response
}
//~~

//~~struct GetParams
// Optional parameters for the `get` function.
type GetParams* = struct {
    userAgent: str
    headers: []str
}
//~~

fn umc__get(url: str, params: ^GetParams, resp: ^Response, typeof: ^void)

//~~fn get
// Make a GET request.
fn get*(url: str, params: GetParams = {}): Response {
//~~
    resp := Response{}
    umc__get(url, ¶ms, &resp, typeptr([]char))
    return resp
}

//~~fn encodeUrl
// Encode a URL with query parameters.
fn encodeUrl*(url: str, args: map[str]any): str {
//~~
    url += "?"
    for k,v in args {
        s := sprintf("%v", v)
        if ^str(v) != null {
            s = str(v)
        }
        url += str(k) + "=" + s + "&"
    }
    
    return slice(url, 0, len(url)-1) // remove last &
}

//~~struct PostParams
// Optional parameters for the `post` function.
type PostParams* = struct {
    userAgent: str
    headers: []str
}
//~~

fn umc__post(url: str, body: ^[]char, params: ^PostParams, out: ^Response, typeof: ^void)
    
//~~fn post
// Make a POST request.
fn post*(url: str, body: []char, params: PostParams = {}): Response {
//~~
    resp := Response{}
    umc__post(url, &body, ¶ms, &resp, typeptr([]char))
    return resp
}

//~~Example
fn main() {
    resp := get(
        encodeUrl("https://httpbin.org/get", {
            "foo": "bar",
            "baz": 42
        }),
        {
            userAgent: "my user agent"
        }
    )

    printf("Ok: %v\n", resp.ok)
    printf("Error: %v\n", resp.err)
    printf("Code: %v\n", resp.status)
    printf("Content type: %v\n", resp.contentType)
    printf("Body: %v\n", str(resp.body))

    resp = post(
        "https://httpbin.org/post",
				[]char("Hello world"),
        {
            userAgent: "my user agent",
            headers: { "Content-Type: text/plain" }
        }
    ) 

    printf("Ok: %v\n", resp.ok)
    printf("Error: %v\n", resp.err)
    printf("Code: %v\n", resp.status)
    printf("Content type: %v\n", resp.contentType)
    printf("Body: %v\n", str(resp.body))
}
//~~