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.
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.
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.