Available since v0.4.0
local http = require "http"
http.request(method, url, options)
Performs an HTTP request and returns a table with response details. The default user agent is tinysse/<version>
.
local res = http.request(
"GET",
"http://httpbin.org/get",
{
args = {key = "value"}, -- Appends ?key=value to the URL
headers = {accept = "application/json"}
}
)
print("Status:", res.status)
print("Headers:", res.headers)
print("Body:", res.body)
Supports standard HTTP methods: GET
, HEAD
, OPTIONS
, POST
, PUT
, PATCH
, and DELETE
.
local res = http.request(
"POST",
"https://httpbin.org/post",
{
headers = {["content-type"] = "application/json"},
body = json {key = "value"}
}
)
print("Status:", res.status)
print("Body:", res.body)
These functions provide shorthand for common HTTP methods.
http.get(url, opts)
http.head(url, opts)
http.options(url, opts)
http.post(url, opts)
http.put(url, opts)
http.patch(url, opts)
http.delete(url, opts)
An http.agent
allows reuse of the internal connection pool and applies specified options (unless overridden per request).
local agent = http.agent {
headers = {
["user-agent"] = "custom-user-agent",
["content-type"] = "application/json"
}
}
local res = agent:post("https://httpbin.org/post", {
body = json {key = "value"}
})
NOTE: The HTTP package does not presently support streaming request or response bodies. The entire body is always buffered into server memory.