HttpResponse Lua Reference Documentation
chilkat.HttpResponse
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.HttpResponse
Read the HTTP status code, status line, and related response information
to determine whether the request succeeded, redirected, or failed.
Inspect response headers, retrieve individual header values, and access
cookies returned by the server.
Get the response body as raw bytes or decoded text, using the response
charset information when appropriate.
Load the body directly into
Access redirect-related information such as the redirect URL when the
server responds with a redirect status.
Save body content, inspect the full MIME-style response, and use response
metadata for diagnostics, logging, or API troubleshooting.
For an extended overview, see
HttpResponse Class Overview.
Inspect HTTP status, headers, cookies, redirects, and response bodies.
Chilkat.HttpResponse represents an HTTP response returned by
certain Chilkat.Http methods. It provides access to the response
status line, status code, headers, cookies, charset, content length, body
bytes, body text, full MIME response, redirect URL, and helper methods for
saving or loading the response body into Chilkat objects such as
BinData, StringBuilder, JsonObject,
JsonArray, and Xml.
Status and reason
Headers and cookies
Body as text or bytes
Structured body loading
JsonObject,
JsonArray, Xml, BinData, or
StringBuilder objects for further processing.
Redirect information
Save and inspect responses
Chilkat.Http, check the
StatusCode, inspect headers or cookies as needed, then retrieve
the body in the form best suited to the response content: text, bytes,
BinData, StringBuilder, JSON, or XML. Use
HttpResponse for response inspection; use Chilkat.Http
for the actual network request.
Object Creation
local chilkat = require("chilkat")
local http_response = chilkat.HttpResponse.new()Creates the object (chilkat.HttpResponse() does the same). Lua's garbage collector frees it when it is no longer referenced; http_response:dispose() frees it at once, and in Lua 5.4 and later local http_response <close> = chilkat.HttpResponse.new() frees it when the block ends. Calling a method of a disposed object raises an error.
Errors
A method that fails returns nil followed by the error text, as io.open does: local body, err = http_response:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.
Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.
Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (http_response.verbose_logging = true) and methods use the colon (http_response:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.
Properties
BodyQP
local strVal = http_response.body_qp
The same as the Body property, but returned as a quoted-printable encoded string.
topBodyStr
local strVal = http_response.body_str
The response body returned as a string.
topCharset
local strVal = http_response.charset
The response charset, such as iso-8859-1, if applicable. Obviously, responses for GIF and JPG files will not have a charset.
ContentLength
local intVal = http_response.content_length
The content length of the response, in bytes.
topContentLength64
local intVal = http_response.content_length64
The content length of the response, in number of bytes, returned as a 64-bit integer.
topDateStr
local strVal = http_response.date_str
Returns the content of the Date response header field in RFC822 date/time string format.
topDebugLogFilePath
local strVal = http_response.debug_log_file_path
http_response.debug_log_file_path = strVal
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
Domain
local strVal = http_response.domain
The domain of the HTTP server that created this response.
topFinalRedirectUrl
local strVal = http_response.final_redirect_url
Returns the redirect URL for 301/302 responses.
topFullMime
local strVal = http_response.full_mime
Header
local strVal = http_response.header
The full text of the response header.
topLastErrorHtml
local strVal = http_response.last_error_html
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
local strVal = http_response.last_error_text
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
local strVal = http_response.last_error_xml
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
local boolVal = http_response.last_method_success
http_response.last_method_success = boolVal
Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
NumCookies
local intVal = http_response.num_cookies
The number of cookies included in the response.
topNumHeaderFields
local intVal = http_response.num_header_fields
The number of response header fields.
topStatusCode
local intVal = http_response.status_code
The status code (as an integer) from the first line of an HTTP response. If the StatusLine = HTTP/1.0 200 OK, the response status code returned is 200.
StatusLine
local strVal = http_response.status_line
The first line of an HTTP response, such as HTTP/1.0 200 OK.
StatusText
local strVal = http_response.status_text
The text that follows the status code in the 1st line of the HTTP response. For example, i the first line of an HTTP response is HTTP/1.0 200 OK, then this property contains OK.
UncommonOptions
local strVal = http_response.uncommon_options
http_response.uncommon_options = strVal
This is a catch-all property to be used for uncommon needs. Can be set to a list of the following comma separated keywords:
TakeResponseBody- Introduced in v9.5.0.89. The GetBody* methods will transfer the body to the target object instead of copying the contents. This can be useful if the response body is extremely large because it will reduce the overall memory requirement.
VerboseLogging
local boolVal = http_response.verbose_logging
http_response.verbose_logging = boolVal
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
local strVal = http_response.version
Methods
GetBodyBd
-- ok is true, or nil and err on failure
local ok, err = http_response:get_body_bd(bin_data)
Copies the response body to a BinData object.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetBodyJarr
-- ok is true, or nil and err on failure
local ok, err = http_response:get_body_jarr(jarr)
Loads the JSON array response body into a JsonArray This method should only be called when it is known that the response body contains a JSON array (beginning with a square bracket '[' and ending with a square bracket ']')
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetBodyJson
-- ok is true, or nil and err on failure
local ok, err = http_response:get_body_json(json)
Loads the JSON response body into a JsonObject. This method should only be called when it is known that the response body contains JSON (beginning with a curly brace '{' and ending with a curly brace '}')
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetBodySb
-- ok is true, or nil and err on failure
local ok, err = http_response:get_body_sb(sb)
Copies the response body to a Chilkat StringBuilder object.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetBodyXml
-- ok is true, or nil and err on failure
local ok, err = http_response:get_body_xml(xml)
Loads the XML response body into an XML object. This method should only be called when it is known that the response body contains XML.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetCookieDomain
-- str is a string, or nil and err on failure
local str, err = http_response:get_cookie_domain(index)
Returns the domain of the Nth cookie in the response. Indexing begins at 0. The number of response cookies is specified in the NumCookies property.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetCookieExpiresStr
-- str is a string, or nil and err on failure
local str, err = http_response:get_cookie_expires_str(index)
Returns the expiration date/time of the Nth cookie in the response. Indexing begins at 0. The number of response cookies is specified in the NumCookies property.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetCookieName
-- str is a string, or nil and err on failure
local str, err = http_response:get_cookie_name(index)
Returns the name of the Nth cookie returned in the response. Indexing begins at 0. The number of response cookies is specified in the NumCookies property.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetCookiePath
-- str is a string, or nil and err on failure
local str, err = http_response:get_cookie_path(index)
Returns the path of the Nth cookie returned in the response. Indexing begins at 0. The number of response cookies is specified in the NumCookies property.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetCookieValue
-- str is a string, or nil and err on failure
local str, err = http_response:get_cookie_value(index)
Returns the value of the Nth cookie returned in the response. Indexing begins at 0. The number of response cookies is specified in the NumCookies property.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetHeaderField
-- str is a string, or nil and err on failure
local str, err = http_response:get_header_field(field_name)
Returns the value of a response header field accessed by field name.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetHeaderFieldAttr
-- attr_name is a string
-- str is a string, or nil and err on failure
local str, err = http_response:get_header_field_attr(field_name, attr_name)
Returns a response header field attribute. As an example, the response charset is simply the GetHeaderFieldAttr(content-type,charset)
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetHeaderName
-- str is a string, or nil and err on failure
local str, err = http_response:get_header_name(index)
Gets the name of the Nth response header field. Indexing begins at 0. The number of response headers is specified by the NumHeaderFields property.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetHeaderValue
-- str is a string, or nil and err on failure
local str, err = http_response:get_header_value(index)
Gets the value of the Nth response header field. Indexing begins at 0. The number of response headers is specified by the NumHeaderFields property.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SaveBodyBinary
-- ok is true, or nil and err on failure
local ok, err = http_response:save_body_binary(path)
Saves the body of the HTTP response to a file.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SaveBodyText
-- path is a string
-- ok is true, or nil and err on failure
local ok, err = http_response:save_body_text(b_crlf, path)
Saves the HTTP response body to a file. This method provides control over CRLF vs bare-LF line-endings. If b_crlf is true, then line endings are automatically converted to CRLF if necessary. If b_crlf is false, then line-endings are automatically converted to bare-LF's (Unix style) if necessary.
To save the HTTP response body exactly as-is (with no line-ending manipulation), then call SaveBodyBinary.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
UrlEncParamValue
-- param_name is a string
-- str is a string, or nil and err on failure
local str, err = http_response:url_enc_param_value(encoded_param_string, param_name)
Convenience method for parsing a param's value out of a URL-encoded param string. For example, if a caller passes the following string in encoded_param_string:
oauth_token=ABC&oauth_token_secret=123&oauth_callback_confirmed=trueand
oauth_token_secret in param_name, then the return value would be 123.Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.