Mostly inspired from HTTPRequest node's implementation, brings extra features and improvements. (WIP)
While I understand that HTTPRequest
requires the SceneTree loop to function which is why it is in a form of a Node, there are several reasons to avoid it:
- It does not reuse HTTPClient between requests.
- It closes and opens a new connection for each redirection even if the redirect
host:port
is the same. - No way of keeping an alive connection.
- Do not use Node for everything.
Ok cool, but shouldn't some of these be reported to Godot's issue tracker ? Yes, I agree however I am not interested in using HTTPRequest
in most cases, or at least until it gets a replacement with a more elegant implementation.
- Keep-Alive connections, which reduces the overhead when sending requests to the same server.
- HTTP connection pool, to send multiple requests by reusing pre-instantiated clients.
- Automatic gzip decompression.
- Idle connection timeout.
- Download streaming.
var client := SimpleHTTPClient.new()
var url := "https://httpbin.org/headers"
var validate_ssl := true
var method := HTTPClient.METHOD_GET
var headers := {
"Name": "Value"
}
var response: HTTPResponse = yield(client.request_async(url, headers, validate_ssl, method), "completed")
if response.successful():
print(response.body.get_string_from_utf8())
if you are willing to reuse the same client to request to the same server, set keep_alive
to true
, to close the connection call close()
(automatically called when the instance is freed)
Requesting large files/payloads must be streamed. The following is an example to download Godot 3.4.4.stable
source and save it to a file:
var client := SimpleHTTPClient.new()
client.stream = true
var url: String = "https://downloads.tuxfamily.org/godotengine/3.4.4/godot-3.4.4-stable.tar.xz"
var file := File.new()
if file.open(url.get_file(), File.WRITE) != OK:
push_error("Failed opening file")
return
client.request_async(url)
print("Downloading...")
while client.requesting:
var stream: Array = yield(client, "response_stream")
var chunk: PoolByteArray = stream[1]
file.store_buffer(chunk)
print("Completed")
file.close()