Standard library · Network

net/httpx

Sending a raw HTTP request with headers of your own and taking the response as text or bytes.

import net.httpx
On this page
  1. Types
  2. Functions

This package is a thin layer under net/http: you write the request line and the headers yourself and get the response untouched. It is what API clients that want full control of the headers are built on, such as the cloud service packages.

Types

Response

struct Response
An HTTP response.
NameTypeDescription
statusint = 0The status code.
headstr = ""The response headers, untouched.
bodystr = ""The body.
okbool = falseThe request was sent and the response was read.
errorstr = ""The error message.
net-httpx-Send.salam
import tls
import net.httpx
import net.tcp
import mem

mut server := tcp.Server {}

func responder():
    repeat 3:
        conn := tcp.Accept(server)
        _ := tcp.Read(conn, 4096)
        tcp.WriteHTTP(conn, "200 OK", "text/plain", "hello from the server")
        tcp.Close(conn)
    end
end

func main:
    remote := httpx.ParseEndpoint("https://api.example.com:8443")
    println remote.ok, remote.host, remote.port, remote.secure
    server = tcp.Bind(0)
    t := spawn(responder)
    local := httpx.ParseEndpoint("http://127.0.0.1:" + tcp.Port(server))
    head := "GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n"
    r := httpx.SendText(local, head, "")
    println r.ok, r.status, r.body, "[" + r.error + "]"
    println httpx.HeaderValue(r.head, "content-type")
    mut raw := httpx.SendRawTimeout(local, head, null, 0, 2000)
    println raw.ok, raw.status, raw.len, httpx.RawText(raw)
    httpx.FreeRaw(raw)
    mut cfg := tls.NewConfig("127.0.0.1")
    cfg.ca_file = "/etc/ssl/certs/ca-certificates.crt"
    mut secured := httpx.SendRawTLS(local, head, null, 0, 2000, cfg)
    println secured.status, secured.len
    httpx.FreeRaw(secured)
    join(t)
    tcp.CloseServer(server)
    println httpx.RawLimit() > 0
    httpx.SetRawLimit(1024 * 1024)
    println httpx.RawLimit()
    mut n := 0
    p := httpx.CopyOut("abc", n)
    println n
    mem.Free(p)
    r2 := httpx.Send(local, head, null, 0)
    raw2 := httpx.SendRaw(local, head, null, 0)
    println r2.ok, raw2.ok
end
Outputtrue api.example.com 8443 true true 200 hello from the server [] text/plain true 200 21 hello from the server 200 21 true 1048576 3 false false

Endpoint

struct Endpoint
The address of one server: the host, the port and whether it is secure.
NameTypeDescription
hoststr = ""The host.
portint = 443The port; 443 by default.
securebool = trueConnect with TLS.
okbool = falseThe address was read correctly.

The example for this item is shown with the ones above: net-httpx-Send.salam

Raw

struct Raw
A response with a binary body.
NameTypeDescription
statusint = 0The status code.
headstr = ""The headers.
datavoid*The address of the body.
lenint = 0The length of the body.
okbool = falseIt succeeded.
errorstr = ""The error message.

The example for this item is shown with the ones above: net-httpx-Send.salam

Functions

ParseEndpoint

func ParseEndpoint(endpoint: str): Endpoint
Reads an address such as https://api.example.com:8443; the default port is 80 for http and 443 for https.

The example for this item is shown with the ones above: net-httpx-Send.salam

Send

func Send(ep: Endpoint, head: str, body: void*, n: int): Response
Sends the request, with the headers you wrote and the given body, and returns the response.

The example for this item is shown with the ones above: net-httpx-Send.salam

SendText

func SendText(ep: Endpoint, head: str, body: str): Response
Like Send, with a text body.

The example for this item is shown with the ones above: net-httpx-Send.salam

HeaderValue

func HeaderValue(head: str, name: str): str
The value of one header in the header text; the name is case insensitive.

The example for this item is shown with the ones above: net-httpx-Send.salam

CopyOut

func CopyOut(body: str, out_len &: int): void*
Copies a string into fresh memory and returns its length.

The example for this item is shown with the ones above: net-httpx-Send.salam

SendRaw

func SendRaw(ep: Endpoint, head: str, body: void*, n: int): Raw
Like Send, but keeps the response body binary; for images and files.

The example for this item is shown with the ones above: net-httpx-Send.salam

SendRawTimeout

func SendRawTimeout(ep: Endpoint, head: str, body: void*, n: int, timeout_ms: int): Raw
The same, with a timeout.

The example for this item is shown with the ones above: net-httpx-Send.salam

SendRawTLS

func SendRawTLS(ep: Endpoint, head: str, body: void*, n: int, timeout_ms: int, cfg: tls.Config): Raw
Like SendRawTimeout, with your own tls.Config: a private CA in ca_file, or insecure_skip_verify for local testing. A plain http endpoint ignores it.

The example for this item is shown with the ones above: net-httpx-Send.salam

FreeRaw

func FreeRaw(r &: Raw)
Frees the memory of a raw response.

The example for this item is shown with the ones above: net-httpx-Send.salam

RawText

func RawText(r: Raw): str
The body of a raw response as a string.

The example for this item is shown with the ones above: net-httpx-Send.salam

RawLimit

func RawLimit(): int
The greatest body size that is read.

The example for this item is shown with the ones above: net-httpx-Send.salam

SetRawLimit

func SetRawLimit(bytes: int)
Changes that limit.

The example for this item is shown with the ones above: net-httpx-Send.salam