Description
Changelog
Reviews (2)
This addon makes HTTP requests much more convenient to use by introducing the await-syntax and removing the need for signals.
Calling an API:
extends AwaitableHTTPRequest
func _ready() -> void:
var resp := await async_request("https://api.github.com/users/swarkin")
if resp.success() and resp.status_ok():
print(resp.status) # 200
print(resp.headers["content-type"]) # application/json
var json = resp.body_as_json()
print(json["login"]) # Swarkin
Downloading an image:
extends AwaitableHTTPRequest
func _ready() -> void:
var bytes := await request_image()
if bytes.is_empty(): return
var path := OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)+"/image.png"
var file := FileAccess.open(path, FileAccess.WRITE)
if not file:
push_error("Failed to open file.")
return
file.store_buffer(bytes)
print("Downloaded and saved a random image to %s!" % path)
func request_image() -> PackedByteArray:
var resp := await http.async_request("https://picsum.photos/256")
if !resp.success() or resp.status_err():
push_error("Request failed.")
return PackedByteArray()
return resp.bytes
View the included examples.tscn scene for more usage examples.
Changelog for version 2.2.1
No changelog provided for this version.