Struct HTTPRequest

A node with the ability to send HTTP(S) requests.

struct HTTPRequest ;

A node with the ability to send HTTP requests. Uses HTTPClient internally. Can be used to make HTTP requests, i.e. download or upload files or web content via HTTP. Example of contacting a REST API and printing one of its returned fields:

func ready(): # Create an HTTP request node and connect its completion signal. var http_request = HTTPRequest.new() add_child(http_request) http_request.connect("request_completed", self, "http_request_completed")

# Perform the HTTP request. The URL below returns some JSON as of writing. var error = http_request.request("https://httpbin.org/get") if error != OK: push_error("An error occurred in the HTTP request.")

# Called when the HTTP request is completed. func http_request_completed(result, response_code, headers, body): var response = parse_json(body.get_string_from_utf8())

# Will print the user agent string used by the HTTPRequest node (as recognized by httpbin.org). print(response.headers"User-Agent")

Example of loading and displaying an image using HTTPRequest:

func ready(): # Create an HTTP request node and connect its completion signal. var http_request = HTTPRequest.new() add_child(http_request) http_request.connect("request_completed", self, "http_request_completed")

# Perform the HTTP request. The URL below returns a PNG image as of writing. var error = http_request.request("https://via.placeholder.com/512") if error != OK: push_error("An error occurred in the HTTP request.")

# Called when the HTTP request is completed. func http_request_completed(result, response_code, headers, body): var image = Image.new() var error = image.load_png_from_buffer(body) if error != OK: push_error("Couldn't load the image.")

var texture = ImageTexture.new() texture.create_from_image(image)

# Display the image in a TextureRect node. var texture_rect = TextureRect.new() add_child(texture_rect) texture_rect.texture = texture

Properties

NameTypeDescription
bodySizeLimit[get, set] longMaximum allowed size for response bodies.
downloadChunkSize[get, set] longThe size of the buffer used and maximum bytes to read per iteration. See HTTPClient.readChunkSize. Set this to a higher value (e.g. 65536 for 64 KiB) when downloading large files to achieve better speeds at the cost of memory.
downloadFile[get, set] StringThe file to download into. Will output any received file into it.
maxRedirects[get, set] longMaximum number of allowed redirects.
timeout[get, set] long
useThreads[get, set] boolIf true, multithreading is used to improve performance.

Methods

NameDescription
_redirectRequest
_requestDone
_timeout
cancelRequest Cancels the current request.
getBodySize Returns the response body length. Note: Some Web servers may not send a body length. In this case, the value returned will be -1. If using chunked transfer encoding, the body length will also be -1.
getBodySizeLimit
getDownloadChunkSize
getDownloadedBytes Returns the amount of bytes this HTTPRequest downloaded.
getDownloadFile
getHttpClientStatus Returns the current status of the underlying HTTPClient. See HTTPClient.status.
getMaxRedirects
getTimeout
isUsingThreads
request Creates request on the underlying HTTPClient. If there is no configuration errors, it tries to connect using HTTPClient.connectToHost and passes parameters onto HTTPClient.request. Returns constant OK if request is successfully created. (Does not imply that the server has responded), constant ERR_UNCONFIGURED if not in the tree, constant ERR_BUSY if still processing previous request, constant ERR_INVALID_PARAMETER if given string is not a valid URL format, or constant ERR_CANT_CONNECT if not using thread and the HTTPClient cannot connect to host.
setBodySizeLimit
setDownloadChunkSize
setDownloadFile
setMaxRedirects
setTimeout
setUseThreads

Enums

NameDescription
Constants
Result