Summary
Sends an HTTP request and obtains a response.
Syntax
local httpResponse = httpClient:request(url)Parameters
httpClient : A previously created HttpClient object. url : URL of the resource to request.
Returns
httpResponse : A table with the corresponding HTTP response. errorMessage : In case of connection error, a description of the error cause.
Description
Connects and sends a request to an HTTP server indicated by the URL parameter. The result of the operation is returned as a response table containing the following elements:
- content: A string with the body of the response.
- status_code: An integer with the HTTP status code of the response.
- status_message: A string with the text corresponding to the response code.
- server: The name of the server that handled the request.
- content_type: The content type of the response.
- headers: A table with HTTP headers of the response.
In case of a connection or unexpected error, this function will return 2 values: nil, errorMessage . Where errorMessage is a string with a description of the error cause.
Examples
local httpClient = HttpClient.new()
local httpResponse, errorMessage = httpClient:request('http://en.wikipedia.org/wiki/Special:Random')
if not httpResponse then
print('Unable to connect, cause: ' .. errorMessage)
else
print('Content type: ' .. httpResponse.content_type)
print('Server: ' .. httpResponse.server)
print('Language: ' .. httpResponse.headers['Content-Language'])
print('Content: ' .. httpResponse.content)
end
See Also