Module jls.net.http.HttpClient

An HTTP client implementation that enable to send and receive message.

Class HttpClient

HttpClient:new (options) Creates a new HTTP client.
httpClient:fetch (resource[, options]) Sends an HTTP request and receives a response.
httpClient:close ([callback]) Closes this HTTP client.


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('https://www.openssl.org/')
httpClient:fetch('/'):next(function(response)
  print('status code is', response:getStatusCode())
  return response:text()
end):next(function(body)
  print(body)
  httpClient:close()
end)
event:loop()
HttpClient:new (options)
Creates a new HTTP client.

Parameters:

  • options A table describing the client options or the URL.
    • url string The request URL.
    • host string The request hostname. (optional)
    • port number The request port number. (optional)
    • isSecure boolean true to use a secure client. (optional)
    • secureContext table the secure context options. (optional)

Returns:

    a new HTTP client
httpClient:fetch (resource[, options])
Sends an HTTP request and receives a response. The response body must be consumed using its text(), json() or consume() method.

Parameters:

  • resource string The request target path.
  • options A table describing the request options.
    • 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)
    • redirect string How to handle a redirect: follow, error or manual. (optional)

Returns:

    a promise that resolves to the HTTP response
httpClient:close ([callback])
Closes this HTTP client.

Parameters:

  • callback function an optional callback function to use in place of promise. (optional)

Returns:

    jls.lang.Promise a promise that resolves once the client is closed.
generated by LDoc 1.4.6