Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

A Godot HTTP client which is a simplified version of HTTPRequest node

License

Notifications You must be signed in to change notification settings

Abdera7mane/Godot-SimpleHTTPClient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple HTTP Client

Mostly inspired from HTTPRequest node's implementation, brings extra features and improvements. (WIP)

Why not just use HTTPRequest node ?

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:

  1. It does not reuse HTTPClient between requests.
  2. It closes and opens a new connection for each redirection even if the redirect host:port is the same.
  3. No way of keeping an alive connection.
  4. 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.

Features

  1. Keep-Alive connections, which reduces the overhead when sending requests to the same server.
  2. HTTP connection pool, to send multiple requests by reusing pre-instantiated clients.
  3. Automatic gzip decompression.
  4. Idle connection timeout.
  5. Download streaming.

Examples

HTTP GET method:

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)

Streaming:

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()

Releases

No releases published