You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-29 16:16:38 +00:00
Make code example in HTTPRequest classref working
Co-authored-by: Raul Santos <raulsntos@gmail.com>
This commit is contained in:
@@ -15,7 +15,7 @@
|
|||||||
# Create an HTTP request node and connect its completion signal.
|
# Create an HTTP request node and connect its completion signal.
|
||||||
var http_request = HTTPRequest.new()
|
var http_request = HTTPRequest.new()
|
||||||
add_child(http_request)
|
add_child(http_request)
|
||||||
http_request.connect("request_completed", self, "_http_request_completed")
|
http_request.request_completed.connect(self._http_request_completed)
|
||||||
|
|
||||||
# Perform a GET request. The URL below returns JSON as of writing.
|
# Perform a GET request. The URL below returns JSON as of writing.
|
||||||
var error = http_request.request("https://httpbin.org/get")
|
var error = http_request.request("https://httpbin.org/get")
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
# Perform a POST request. The URL below returns JSON as of writing.
|
# Perform a POST request. The URL below returns JSON as of writing.
|
||||||
# Note: Don't make simultaneous requests using a single HTTPRequest node.
|
# Note: Don't make simultaneous requests using a single HTTPRequest node.
|
||||||
# The snippet below is provided for reference only.
|
# The snippet below is provided for reference only.
|
||||||
var body = {"name": "Godette"}
|
var body = JSON.new().stringify({"name": "Godette"})
|
||||||
error = http_request.request("https://httpbin.org/post", [], true, HTTPClient.METHOD_POST, body)
|
error = http_request.request("https://httpbin.org/post", [], true, HTTPClient.METHOD_POST, body)
|
||||||
if error != OK:
|
if error != OK:
|
||||||
push_error("An error occurred in the HTTP request.")
|
push_error("An error occurred in the HTTP request.")
|
||||||
@@ -33,7 +33,9 @@
|
|||||||
|
|
||||||
# Called when the HTTP request is completed.
|
# Called when the HTTP request is completed.
|
||||||
func _http_request_completed(result, response_code, headers, body):
|
func _http_request_completed(result, response_code, headers, body):
|
||||||
var response = parse_json(body.get_string_from_utf8())
|
var json = JSON.new()
|
||||||
|
json.parse(body.get_string_from_utf8())
|
||||||
|
var response = json.get_data()
|
||||||
|
|
||||||
# Will print the user agent string used by the HTTPRequest node (as recognized by httpbin.org).
|
# Will print the user agent string used by the HTTPRequest node (as recognized by httpbin.org).
|
||||||
print(response.headers["User-Agent"])
|
print(response.headers["User-Agent"])
|
||||||
@@ -44,7 +46,7 @@
|
|||||||
// Create an HTTP request node and connect its completion signal.
|
// Create an HTTP request node and connect its completion signal.
|
||||||
var httpRequest = new HTTPRequest();
|
var httpRequest = new HTTPRequest();
|
||||||
AddChild(httpRequest);
|
AddChild(httpRequest);
|
||||||
httpRequest.Connect("request_completed", this, nameof(HttpRequestCompleted));
|
httpRequest.RequestCompleted += HttpRequestCompleted;
|
||||||
|
|
||||||
// Perform a GET request. The URL below returns JSON as of writing.
|
// Perform a GET request. The URL below returns JSON as of writing.
|
||||||
Error error = httpRequest.Request("https://httpbin.org/get");
|
Error error = httpRequest.Request("https://httpbin.org/get");
|
||||||
@@ -56,21 +58,24 @@
|
|||||||
// Perform a POST request. The URL below returns JSON as of writing.
|
// Perform a POST request. The URL below returns JSON as of writing.
|
||||||
// Note: Don't make simultaneous requests using a single HTTPRequest node.
|
// Note: Don't make simultaneous requests using a single HTTPRequest node.
|
||||||
// The snippet below is provided for reference only.
|
// The snippet below is provided for reference only.
|
||||||
string[] body = { "name", "Godette" };
|
string body = new JSON().Stringify(new Godot.Collections.Dictionary
|
||||||
// GDScript to_json is non existent, so we use JSON.Print() here.
|
{
|
||||||
error = httpRequest.Request("https://httpbin.org/post", null, true, HTTPClient.Method.Post, JSON.Print(body));
|
{ "name", "Godette" }
|
||||||
|
});
|
||||||
|
error = httpRequest.Request("https://httpbin.org/post", null, true, HTTPClient.Method.Post, body);
|
||||||
if (error != Error.Ok)
|
if (error != Error.Ok)
|
||||||
{
|
{
|
||||||
GD.PushError("An error occurred in the HTTP request.");
|
GD.PushError("An error occurred in the HTTP request.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Called when the HTTP request is completed.
|
// Called when the HTTP request is completed.
|
||||||
private void HttpRequestCompleted(int result, int response_code, string[] headers, byte[] body)
|
private void HttpRequestCompleted(int result, int response_code, string[] headers, byte[] body)
|
||||||
{
|
{
|
||||||
// GDScript parse_json is non existent so we have to use JSON.parse, which has a slightly different syntax.
|
var json = new JSON();
|
||||||
var response = JSON.Parse(body.GetStringFromUTF8()).Result as Godot.Collections.Dictionary;
|
json.Parse(body.GetStringFromUTF8());
|
||||||
|
var response = json.GetData() as Godot.Collections.Dictionary;
|
||||||
|
|
||||||
// Will print the user agent string used by the HTTPRequest node (as recognized by httpbin.org).
|
// Will print the user agent string used by the HTTPRequest node (as recognized by httpbin.org).
|
||||||
GD.Print((response["headers"] as Godot.Collections.Dictionary)["User-Agent"]);
|
GD.Print((response["headers"] as Godot.Collections.Dictionary)["User-Agent"]);
|
||||||
}
|
}
|
||||||
@@ -83,7 +88,7 @@
|
|||||||
# Create an HTTP request node and connect its completion signal.
|
# Create an HTTP request node and connect its completion signal.
|
||||||
var http_request = HTTPRequest.new()
|
var http_request = HTTPRequest.new()
|
||||||
add_child(http_request)
|
add_child(http_request)
|
||||||
http_request.connect("request_completed", self, "_http_request_completed")
|
http_request.request_completed.connect(self._http_request_completed)
|
||||||
|
|
||||||
# Perform the HTTP request. The URL below returns a PNG image as of writing.
|
# Perform the HTTP request. The URL below returns a PNG image as of writing.
|
||||||
var error = http_request.request("https://via.placeholder.com/512")
|
var error = http_request.request("https://via.placeholder.com/512")
|
||||||
@@ -115,7 +120,7 @@
|
|||||||
// Create an HTTP request node and connect its completion signal.
|
// Create an HTTP request node and connect its completion signal.
|
||||||
var httpRequest = new HTTPRequest();
|
var httpRequest = new HTTPRequest();
|
||||||
AddChild(httpRequest);
|
AddChild(httpRequest);
|
||||||
httpRequest.Connect("request_completed", this, nameof(HttpRequestCompleted));
|
httpRequest.RequestCompleted += HttpRequestCompleted;
|
||||||
|
|
||||||
// Perform the HTTP request. The URL below returns a PNG image as of writing.
|
// Perform the HTTP request. The URL below returns a PNG image as of writing.
|
||||||
Error error = httpRequest.Request("https://via.placeholder.com/512");
|
Error error = httpRequest.Request("https://via.placeholder.com/512");
|
||||||
@@ -125,7 +130,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Called when the HTTP request is completed.
|
// Called when the HTTP request is completed.
|
||||||
private void HttpRequestCompleted(int result, int response_code, string[] headers, byte[] body)
|
private void HttpRequestCompleted(int result, int response_code, string[] headers, byte[] body)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user