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

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

Read the HTTP status code, status line, and related response information to determine whether the request succeeded, redirected, or failed.

Headers and cookies

Inspect response headers, retrieve individual header values, and access cookies returned by the server.

Body as text or bytes

Get the response body as raw bytes or decoded text, using the response charset information when appropriate.

Structured body loading

Load the body directly into JsonObject, JsonArray, Xml, BinData, or StringBuilder objects for further processing.

Redirect information

Access redirect-related information such as the redirect URL when the server responds with a redirect status.

Save and inspect responses

Save body content, inspect the full MIME-style response, and use response metadata for diagnostics, logging, or API troubleshooting.

Common pattern: Send a request with 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()
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
-- strVal is a string (read-only)
local strVal = http_response.body_qp

The same as the Body property, but returned as a quoted-printable encoded string.

top
BodyStr
-- strVal is a string (read-only)
local strVal = http_response.body_str

The response body returned as a string.

top
Charset
-- strVal is a string (read-only)
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.

top
ContentLength
-- intVal is an integer (read-only)
local intVal = http_response.content_length

The content length of the response, in bytes.

top
ContentLength64
-- intVal is an integer (read-only)
local intVal = http_response.content_length64

The content length of the response, in number of bytes, returned as a 64-bit integer.

top
DateStr
-- strVal is a string (read-only)
local strVal = http_response.date_str

Returns the content of the Date response header field in RFC822 date/time string format.

top
DebugLogFilePath
-- strVal is a string
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.

More Information and Examples
top
Domain
-- strVal is a string (read-only)
local strVal = http_response.domain

The domain of the HTTP server that created this response.

top
FinalRedirectUrl
-- strVal is a string (read-only)
local strVal = http_response.final_redirect_url
Introduced in version 9.5.0.49

Returns the redirect URL for 301/302 responses.

top
FullMime
-- strVal is a string (read-only)
local strVal = http_response.full_mime

Returns the full MIME (header + body) of the HTTP response.

top
Header
-- strVal is a string (read-only)
local strVal = http_response.header

The full text of the response header.

top
LastErrorHtml
-- strVal is a string (read-only)
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.

top
LastErrorText
-- strVal is a string (read-only)
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.

top
LastErrorXml
-- strVal is a string (read-only)
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.

top
LastMethodSuccess
-- boolVal is a boolean
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.

top
NumCookies
-- intVal is an integer (read-only)
local intVal = http_response.num_cookies

The number of cookies included in the response.

top
NumHeaderFields
-- intVal is an integer (read-only)
local intVal = http_response.num_header_fields

The number of response header fields.

top
StatusCode
-- intVal is an integer (read-only)
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.

top
StatusLine
-- strVal is a string (read-only)
local strVal = http_response.status_line

The first line of an HTTP response, such as HTTP/1.0 200 OK.

top
StatusText
-- strVal is a string (read-only)
local strVal = http_response.status_text
Introduced in version 9.5.0.49

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.

top
UncommonOptions
-- strVal is a string
local strVal = http_response.uncommon_options
http_response.uncommon_options = strVal
Introduced in version 9.5.0.89

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.

top
VerboseLogging
-- boolVal is a boolean
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.

top
Version
-- strVal is a string (read-only)
local strVal = http_response.version

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

GetBodyBd
-- bin_data is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = http_response:get_body_bd(bin_data)
Introduced in version 9.5.0.64

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.

More Information and Examples
top
GetBodyJarr
-- jarr is a chilkat.JsonArray
-- ok is true, or nil and err on failure
local ok, err = http_response:get_body_jarr(jarr)
Introduced in version 9.5.0.89

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.

More Information and Examples
top
GetBodyJson
-- json is a chilkat.JsonObject
-- ok is true, or nil and err on failure
local ok, err = http_response:get_body_json(json)
Introduced in version 9.5.0.89

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.

More Information and Examples
top
GetBodySb
-- sb is a chilkat.StringBuilder
-- ok is true, or nil and err on failure
local ok, err = http_response:get_body_sb(sb)
Introduced in version 9.5.0.64

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.

top
GetBodyXml
-- xml is a chilkat.Xml
-- ok is true, or nil and err on failure
local ok, err = http_response:get_body_xml(xml)
Introduced in version 9.5.0.89

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.

More Information and Examples
top
GetCookieDomain
-- index is an integer
-- 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.

top
GetCookieExpiresStr
-- index is an integer
-- 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.

top
GetCookieName
-- index is an integer
-- 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.

top
GetCookiePath
-- index is an integer
-- 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.

top
GetCookieValue
-- index is an integer
-- 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.

top
GetHeaderField
-- field_name is a string
-- 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.

top
GetHeaderFieldAttr
-- field_name is a string
-- 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.

top
GetHeaderName
-- index is an integer
-- 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.

top
GetHeaderValue
-- index is an integer
-- 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.

top
SaveBodyBinary
-- path is a string
-- 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.

top
SaveBodyText
-- b_crlf is a boolean
-- 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.

top
UrlEncParamValue
-- encoded_param_string is a string
-- 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=true
and 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.

top