http

A small HTTP client. Body-returning methods give back the response body as a string; head and status return the status code as an int. Import it with import http.

Looking for the built-in HTTP server? http.serve is documented separately at /docs/http-server. This page covers the client functions.

Functions

FunctionSignatureDescription
gethttp.get(url: string) → stringGET request, returns the response body
posthttp.post(url: string, body: string) → stringPOST with a JSON body, returns the body
puthttp.put(url: string, body: string) → stringPUT with a JSON body, returns the body
patchhttp.patch(url: string, body: string) → stringPATCH with a JSON body, returns the body
deletehttp.delete(url: string) → stringDELETE request, returns the body
headhttp.head(url: string) → intHEAD request, returns the status code
statushttp.status(url: string) → intGET request, returns only the status code
requesthttp.request(method: string, url: string, body: string, headers: string[]) → stringGeneric request with custom "Key: Value" headers; returns the body

post, put, and patch send Content-Type: application/json when the body is non-empty. request does the same unless you supply your own Content-Type header; pass "" as the body for methods that carry none.

Example

import http

// fetch a resource
let body = http.get("https://api.example.com/items")
print(body)

// post JSON
let created = http.post("https://api.example.com/items", "{\"name\":\"box\"}")

// check existence without downloading the body
if http.status("https://api.example.com/items/1") == 200 {
  print("exists")
}

// custom method and headers
let result = http.request(
  "DELETE",
  "https://api.example.com/items/1",
  "",
  ["Authorization: Bearer token123"],
)
Standard library · View as Markdown · llms-full.txt