http

Edit

The lovr-http plugin performs HTTP requests.

First, require the plugin and save it into a variable: local http = require 'http'.

The module has one function:

status, data, headers = http.request(url, [options])

This will perform an HTTP request and block until the request is complete.

Arguments

url is the URL to request. If it doesn't have a protocol, then http:// will be added.

options is optional, and is used for advanced request settings.

options.method is the HTTP method to use, also called the verb. GET is used by default if there's no data in the request, otherwise it defauls to POST. It will be converted to all-caps.

options.data is the data to send to the server, also called the body. It can be a few different types:

When options.data is set, the Content-Type request header will default to application/x-www-urlencoded unless it's set to something else.

options.headers is a table of request headers to send to the server. Pairs in the table will only be used if the key is a string and the value is a string or number.

Returns

If an error occurs, the function returns nil, errormessage.

Otherwise, 3 values are returned:

Notes

On Linux, this module requires the curl library to be installed. Example on Debian-based distributions:

sudo apt install libcurl4

Example

local http = require 'http'

local status, data, headers = http.request('https://zombo.com')

print('welcome')
print(status)
print(data)
print('headers:')
for k, v in pairs(headers) do
  print('\t' .. k, v)
end