Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

uji.http

uji.http.request(opts, on_done)

Sends an HTTP request and returns a function that cancels it. on_done receives a response table with status, headers and body, or nil and an error message. Header names are lower case.

OptionTypeMeaning
urlstringRequired. The URL.
methodstringThe HTTP method. The default is "GET".
headerstableRequest headers, name to value.
bodystringThe request body.
timeoutnumberSeconds before the request fails. The default is 30 for a plain request and none for a streamed one.
on_linefunctionStreams the response. uji calls it with each line of the body as it arrives. Return false for a line that is not progress, such as a keep-alive.
idlenumberFor a streamed request, the seconds without progress before uji gives up. The default is 120.

A non-2xx status still counts as a response. Check status yourself. An invalid URL, method or header raises an error.

uji.http.request({
  url = "https://api.github.com/repos/uji-labs/uji",
  headers = { Accept = "application/vnd.github+json" },
}, function(response, err)
  if not response then
    uji.notify("request failed: " .. err)
  elseif response.status == 200 then
    uji.notify(uji.json.decode(response.body).description)
  end
end)