The HTTPRequest node provides an easy way to make HTTP and HTTPS requests to web servers and REST APIs. It supports GET, POST, PUT, DELETE, and other HTTP methods, along with file downloads and uploads.
HTTPRequest uses HTTPClient internally and provides a simpler, node-based interface for common HTTP operations.
func send_post_request(): var http_request = HTTPRequest.new() add_child(http_request) http_request.request_completed.connect(_on_post_completed) # Prepare POST data var data = { "username": "player123", "score": 1000, "level": 5 } var json = JSON.new() var body = json.stringify(data) # Set headers var headers = ["Content-Type: application/json"] # Send POST request var error = http_request.request( "https://api.example.com/scores", headers, HTTPClient.METHOD_POST, body ) if error != OK: push_error("Failed to send POST request")func _on_post_completed(result, response_code, headers, body): if result == HTTPRequest.RESULT_SUCCESS and response_code == 201: print("Score submitted successfully!") else: print("Failed to submit score: ", response_code)
private void SendPostRequest(){ var httpRequest = new HttpRequest(); AddChild(httpRequest); httpRequest.RequestCompleted += OnPostCompleted; // Prepare POST data var data = new Godot.Collections.Dictionary { { "username", "player123" }, { "score", 1000 }, { "level", 5 } }; var json = new Json(); string body = json.Stringify(data); // Set headers string[] headers = { "Content-Type: application/json" }; // Send POST request Error error = httpRequest.Request( "https://api.example.com/scores", headers, HttpClient.Method.Post, body ); if (error != Error.Ok) { GD.PushError("Failed to send POST request"); }}private void OnPostCompleted(long result, long responseCode, string[] headers, byte[] body){ if (result == (long)HttpRequest.Result.Success && responseCode == 201) { GD.Print("Score submitted successfully!"); } else { GD.Print($"Failed to submit score: {responseCode}"); }}
var http_request: HTTPRequestfunc _ready(): http_request = HTTPRequest.new() add_child(http_request) http_request.download_file = "user://large_file.zip" http_request.request("https://example.com/large_file.zip")func _process(delta): if http_request: var downloaded = http_request.get_downloaded_bytes() var total = http_request.get_body_size() if total > 0: var progress = float(downloaded) / float(total) * 100.0 print("Download progress: ", progress, "%")
Files are downloaded to the specified path. Use user:// for writable user directory.
func parse_json_response(body: PackedByteArray): var json = JSON.new() var error = json.parse(body.get_string_from_utf8()) if error != OK: push_error("Failed to parse JSON: " + json.get_error_message()) return null return json.get_data()func _on_request_completed(result, response_code, headers, body): var data = parse_json_response(body) if data and data.has("players"): for player in data["players"]: print("Player: ", player["name"], " Score: ", player["score"])