Module jls.net.http.HttpClient

An HTTP client implementation that enable to send request and receive response.

Class HttpClient

HttpClient:new (options) Creates a new HTTP client.
httpClient:connect () Connects this HTTP client.
httpClient:close () Closes this HTTP client.
httpClient:sendReceive () Sends the request then receives the response.


Class HttpClient

The HttpClient class enables to send an HTTP request.

Usage:

local event = require('jls.lang.event')
local HttpClient = require('jls.net.http.HttpClient')
local httpClient = HttpClient:new({
  url = 'https://www.openssl.org/',
  method = 'GET',
  headers = {}
})
httpClient:connect():next(function()
  return httpClient:sendReceive()
end):next(function(response)
  print('status code is', response:getStatusCode())
  print(response:getBody())
  httpClient:close()
end)
event:loop()
HttpClient:new (options)
Creates a new HTTP client.

Parameters:

  • options A table describing the client options.
    • url string The request URL.
    • host string The request hostname. (optional)
    • port number The request port number. (optional)
    • target string The request target path. (optional)
    • method string The HTTP method, default is GET. (optional)
    • headers table The HTTP request headers. (optional)
    • body string The HTTP request body, default is empty body. (optional)
    • followRedirect boolean true to follow redirections. (optional)
    • maxRedirectCount number The maximum of redirections, default is 3. (optional)

Returns:

    a new HTTP client
httpClient:connect ()
Connects this HTTP client.

Returns:

    jls.lang.Promise a promise that resolves once this client is connected.
httpClient:close ()
Closes this HTTP client.

Returns:

    jls.lang.Promise a promise that resolves once the client is closed.
httpClient:sendReceive ()
Sends the request then receives the response.

Returns:

    jls.lang.Promise a promise that resolves to the HttpResponse received.
generated by LDoc 1.4.6 Last updated 2022-01-22 16:32:56