net/httpx
Sending a raw HTTP request with headers of your own and taking the response as text or bytes.
import net.httpx
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| Name | Type | Description |
|---|---|---|
status | int = 0 | The status code. |
head | str = "" | The response headers, untouched. |
body | str = "" | The body. |
ok | bool = false | The request was sent and the response was read. |
error | str = "" | The error message. |
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
endEndpoint
struct Endpoint| Name | Type | Description |
|---|---|---|
host | str = "" | The host. |
port | int = 443 | The port; 443 by default. |
secure | bool = true | Connect with TLS. |
ok | bool = false | The address was read correctly. |
The example for this item is shown with the ones above: net-httpx-Send.salam
Raw
struct Raw| Name | Type | Description |
|---|---|---|
status | int = 0 | The status code. |
head | str = "" | The headers. |
data | void* | The address of the body. |
len | int = 0 | The length of the body. |
ok | bool = false | It succeeded. |
error | str = "" | The error message. |
The example for this item is shown with the ones above: net-httpx-Send.salam
Functions
ParseEndpoint
func ParseEndpoint(endpoint: str): Endpointhttps://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): ResponseThe example for this item is shown with the ones above: net-httpx-Send.salam
SendText
func SendText(ep: Endpoint, head: str, body: str): ResponseSend, 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): strThe example for this item is shown with the ones above: net-httpx-Send.salam
CopyOut
func CopyOut(body: str, out_len &: int): void*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): RawSend, 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): RawThe 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): RawSendRawTimeout, 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)The example for this item is shown with the ones above: net-httpx-Send.salam
RawText
func RawText(r: Raw): strThe example for this item is shown with the ones above: net-httpx-Send.salam
RawLimit
func RawLimit(): intThe example for this item is shown with the ones above: net-httpx-Send.salam
SetRawLimit
func SetRawLimit(bytes: int)The example for this item is shown with the ones above: net-httpx-Send.salam