lua.wiretasks
local wiretasks=require("wiretasks")
This is a small collection of useful tasks for running via wire. Each task has its own specific module dependencies, eg lua socket is needed for http fetching. This module is usually used in a task start code string passed to wire.tasks. So "require('wiretasks').http_code()" would be used to start the http task.
We also include some wrapper functions that make use of memos and require certain tasks to already be running.
lua.wiretasks.http_code
wire.tasks("http",4,"require('wiretasks').http_code()")
result = wire.memo({
fifo=wire.fifo("http"),
data={
url="http://google.com/",
},
on_result=function() end,
}):resolve()
The code we run in "http" tasks to handle http requests via memos.
This function requires the following libraries to be available.
require("socket.http")
require("ltn12")
Every memo sent is a http fetch request, the keys set in memo.data control the type of request.
data.url = "http://google.com/"
The url we will be talking to. This is all you need for a basic GET.
data.get = { a=1 , b=2 }
If this exists then we send a GET request with this table in the query string, eg append "?a=1&b=2" to the url.
data.json = { a=1 , b=2 }
If this exists then we encode as json and send as body of a POST also setting "Content-Type" to "application/json" in data.headers
data.post = { a=1 , b=2 }
If this exists then we encode as a query string and send as body of a POST also setting "Content-Type" to "application/x-www-form-urlencoded" in data.headers
data.body = "a=1&b=2"
Force a request body string.
data.method = "GET"
Force a request method.
data.headers = { ["Content-Type"]="application/json" , ... }
Force extra headers.
result.code
result.status
result.headers
result.body
The result of the http.request, may also raise an error for catastrophic failures, eg host not found.
Many things can go wrong so always check that result.fail is not set and the result.code is as expected ( eg 200 ) and only then is it safe to read the result.body