HTTP client
The http module is also a client. Each call returns the response body as a
string (or, for head/status, the status code).
import http
// GET
let post = http.get("https://jsonplaceholder.typicode.com/posts/1")
println(post)
// POST with a JSON body
let newPost = "{\"title\":\"Langoost\",\"body\":\"Fast scripting\",\"userId\":1}"
let created = http.post("https://jsonplaceholder.typicode.com/posts", newPost)
println(created)
// HEAD — check existence without downloading the body
let status = http.head("https://jsonplaceholder.typicode.com/posts/1")
if status == 200 {
println("resource exists")
}
// Generic request with custom headers (headers is a string[])
let resp = http.request("GET", "https://jsonplaceholder.typicode.com/posts/2", "", [
"Accept: application/json",
"Authorization: Bearer my-token"
])
println(resp)
Available methods: get, post, put, patch, delete, head, status,
and the generic request(method, url, body, headers).