diff --git a/test_godot.gd b/test_godot.gd new file mode 100644 index 0000000..2bc78fd --- /dev/null +++ b/test_godot.gd @@ -0,0 +1,18 @@ +extends Node2D + +@onready var http_request: HTTPRequest = $HTTPRequest + +func _ready() -> void: + http_request.request_completed.connect(_request_data) + http_request.request("http://localhost:9450/api?getVariable=somevariable") + +func _request_data(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void: + print("Result: %d" % result) + print("Response Code: %d" % response_code) + print("Headers:", JSON.stringify(headers, "\t")) + var data := body.get_string_from_utf8() + var d: Dictionary = JSON.parse_string(data) + print("Body (str): ", body.get_string_from_utf8()) + print("Body (JSON): ", JSON.stringify(d, "\t")) + print("Completed") + diff --git a/test_godot.gd.uid b/test_godot.gd.uid new file mode 100644 index 0000000..772b433 --- /dev/null +++ b/test_godot.gd.uid @@ -0,0 +1 @@ +uid://byipe2kjrnbqu diff --git a/test_godot.tscn b/test_godot.tscn new file mode 100644 index 0000000..cfcc250 --- /dev/null +++ b/test_godot.tscn @@ -0,0 +1,9 @@ +[gd_scene load_steps=2 format=3 uid="uid://bs53go6esgusj"] + +[ext_resource type="Script" uid="uid://byipe2kjrnbqu" path="res://test_godot.gd" id="1_pyjj8"] + +[node name="TestGodot" type="Node2D"] +script = ExtResource("1_pyjj8") + +[node name="HTTPRequest" type="HTTPRequest" parent="."] +accept_gzip = false diff --git a/test_node.gd b/test_node.gd index 920e042..db593f6 100644 --- a/test_node.gd +++ b/test_node.gd @@ -90,7 +90,7 @@ func _send_request(peer: StreamPeer) -> void: if ssl: proto = "https" - var request_str := "GET %s://%s:%d%s HTTP/1.1\r\n" % [proto, host, port, query] + var request_str := "GET %s HTTP/1.1\r\n" % [query] if (ssl and port == 443) or (not ssl and port == 80): request_str += "Host: %s\r\n" % host else: @@ -108,7 +108,7 @@ func _send_request(peer: StreamPeer) -> void: ] request_str += "Accept: %s\r\n\r\n" % accept - print_rich("[color=cyan]Request Headers:",request_str,"[/color]--EOH--") + print_rich("[color=cyan]Request Headers:\n",request_str,"[/color]--EOH--") peer.put_data(request_str.to_utf8_buffer()) func _handle_response(peer: StreamPeer) -> void: diff --git a/test_node.tscn b/test_node.tscn index 71d690a..2bed75b 100644 --- a/test_node.tscn +++ b/test_node.tscn @@ -4,6 +4,6 @@ [node name="TestNode" type="Node2D"] script = ExtResource("1_6uery") -host = "api.github.com" -port = 443 -query = "/repos/godotengine/godot/releases/latest" +host = "localhost" +port = 9450 +query = "/api?getVariable=somevariable"