HTTP is a thin wrapper around Godot's HTTPRequest for talking to a REST API.
It creates the request node for you, sets JSON headers, serializes dictionary
bodies, and routes the result to onComplete / onError callbacks.
HTTP reads two project settings once at load:
custom/api-urlβ base URL, prepended toendpointcustom/api-keyβ sent as thex-api-keyheader
Set them in project.godot:
[custom]
api-url="https://api.your-game.com"
api-key="your-api-key"HTTP.Get(caller, options)HTTP.Post(caller, options)HTTP.Put(caller, options)
caller is the node the temporary HTTPRequest is attached to (usually
self). options is a dictionary:
| key | description |
|---|---|
endpoint |
path appended to custom/api-url (e.g. /score) |
url |
full URL β overrides endpoint + base url |
body |
request body; a Dictionary is JSON-stringified automatically |
onComplete |
func(result, response_code, headers, body) on success |
onError |
func(result, response_code, headers, body) on transport error |
The callbacks receive the raw Godot
request_completed
arguments. body is a PackedByteArray β decode it with
body.get_string_from_utf8().
Router.showLoader()
HTTP.Post(self, {
endpoint = "/score",
body = {playerId = "FieryFox", score = 100},
onError = func(_result, _response_code, _headers, _body):
handleScoreFailure()
Router.hideLoader()
,
onComplete = func(_result, _response_code, _headers, body):
var payload = body.get_string_from_utf8()
var newRecord = __.GetOr(false, 'newRecord', payload)
G.debug('β
[b][color=green]score posted[/color][/b]', {newRecord = newRecord})
Router.hideLoader()
})