esi-lcurl-http-client
-
HTTP Client to perform HTTP requests using the lcurl library.
-
Using lcurl library which documentation can be found here.
-
Supports HTTPS requests via proxy using optional proxy credentials.
Documentation
NEW
NEW(options)
Create a new instance of the HTTPClient with options.
Supported options:
local options = {
proxy = 'proxy.company.com',
proxyCred = 'username:password',
ssl_verifypeer = true
}
If you don’t want to check peer certificate, set ssl_verifypeer
to false
or leave it nil
.
Usage:
local HTTPClient = require('esi-lcurl-http-client')
local options = {}
local httpClient = HTTPClient.NEW(options)
REQUEST
REQUEST(method, url, headers, reqData)
Sends out a HTTP Request.
Parameters: | |
---|---|
method |
like 'GET', 'POST', etc. Can also be a table contain the arguments as fields. |
url |
string containing the URL. |
headers |
(optional) table. |
reqData |
(optional) Request Data can be a string or a table. |
Returns |
table with res.ok, res.code (HTTP Response Codes) and optional res.data and res.headers |
Usage:
local url = 'http://inmationwebapi.company.com:8002/api/checkstatus'
local res = httpClient:REQUEST('GET', url)
return res.ok, res.code, res.data, res.headers
DELETE
DELETE(url, headers, body)
Sends out a HTTP DELETE Request.
Parameters: | |
---|---|
url |
string containing the URL. |
headers |
(optional) table. |
reqData |
(optional) Request Data can be a string or a table. |
Returns |
table with res.ok, res.code (HTTP Response Codes) and optional res.data and res.headers |
Usage:
local res = httpClient:DELETE(url)
return res.ok, res.code, res.data, res.headers
GET
GET(url, headers)
Sends out a HTTP GET Request.
Parameters: | |
---|---|
url |
string containing the URL. |
headers |
(optional) table. |
Returns |
table with res.ok, res.code (HTTP Response Codes) and optional res.data and res.headers |
Usage:
local url = 'http://inmationwebapi.company.com:8002/api/checkstatus'
local res = httpClient:GET(url)
return res.ok, res.code, res.data, res.headers
HEAD
HEAD(url, headers)
Sends out a HTTP HEAD Request which is a GET without response body.
Parameters: | |
---|---|
url |
string containing the URL. |
headers |
(optional) table. |
Returns |
table with res.ok, res.code (HTTP Response Codes) and optional res.data and res.headers |
Usage:
local url = 'http://inmationwebapi.company.com:8002/api/checkstatus'
local res = httpClient:HEAD(url)
return res.ok, res.code, res.headers
OPTIONS
OPTIONS(url, headers, reqData)
Sends out a HTTP OPTIONS Request.
Parameters: | |
---|---|
url |
string containing the URL. |
headers |
(optional) table. |
reqData |
(optional) Request Data can be a string or a table. |
Returns |
table with res.ok, res.code (HTTP Response Codes) and optional res.data and res.headers |
Usage:
local res = httpClient:OPTIONS(url)
return res.ok, res.code, res.data, res.headers
PATCH
PATCH(url, headers, reqData)
Sends out a HTTP PATCH Request.
Parameters: | |
---|---|
url |
string containing the URL. |
headers |
(optional) table. |
reqData |
(optional) Request Data can be a string or a table. |
Returns | table with res.ok, res.code (HTTP Response Codes) and optional res.data and res.headers
Usage:
local reqData = {
msg = "Hello World!"
}
local res = httpClient:PATCH(url, {}, reqData)
return res.ok, res.code, res.data, res.headers
POST
POST(url, headers, reqData)
Sends out a HTTP POST Request.
Parameters: | |
---|---|
url |
string containing the URL. |
headers |
(optional) table. |
reqData |
(optional) Request Data can be a string or a table. |
Returns |
table with res.ok, res.code (HTTP Response Codes) and optional res.data and res.headers |
Usage:
local reqData = {
msg = "Hello World!"
}
local res = httpClient:POST(url, {}, reqData)
return res.ok, res.code, res.data, res.headers
PUT
PUT(url, headers, reqData)
Sends out a HTTP PUT Request.
Parameters: | |
---|---|
url |
string containing the URL. |
headers |
(optional) table. |
reqData |
(optional) Request Data can be a string or a table. |
Returns |
table with res.ok, res.code (HTTP Response Codes) and optional res.data and res.headers |
Usage:
local reqData = {
msg = "Hello World!"
}
local res = httpClient:PUT(url, {}, reqData)
return res.ok, res.code, res.data, res.headers
Examples
Request using Proxy
Making a HTTP request using a proxy server with authentication.
local HTTPClient = require('esi-lcurl-http-client')
local options = {
proxy = 'proxy.company.com',
proxyCred = 'username:password'
}
local httpClient = HTTPClient.NEW(options)
local url = "http://inmationwebapi.company.com:8002/api/checkstatus"
local res = httpClient:GET(url)
Compression
Handle responses with a specific Content-Encoding
. In this example deflate
.
Library LibDeflate
can be found on GitHub.
local HTTPClient = require('esi-lcurl-http-client')
local LibDeflate = require("LibDeflate")
local httpClient = HTTPClient.NEW()
httpClient.ONENCODEDRESPONSEDATA = function(encoded, encoding)
if encoding == 'deflate' then
local decompress_deflate = encoded
local decompressed = nil
repeat
decompress_deflate = LibDeflate:DecompressDeflate(decompress_deflate)
if decompress_deflate ~= nil then
decompressed = decompress_deflate
end
until(decompress_deflate == nil)
return decompressed
end
error(("Unsupported encoding: '%s'"):format(encoding))
end