diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 833fd1adc381..51eabf9f8005 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -68,7 +68,7 @@ Error HTTPClient::_request(Method p_method, const String &p_url, const Vector 0 ? (const uint8_t *)body_utf8.get_data() : nullptr, size); } -String HTTPClient::query_string_from_dict(const Dictionary &p_dict) { +String HTTPClient::query_string_from(const Dictionary &p_dict) { String query = ""; Array keys = p_dict.keys(); for (int i = 0; i < keys.size(); ++i) { @@ -97,6 +97,10 @@ String HTTPClient::query_string_from_dict(const Dictionary &p_dict) { return query.substr(1); } +String HTTPClient::query_string_from_dict(const Dictionary &p_dict) { + return HTTPClient::query_string_from(p_dict); +} + Error HTTPClient::verify_headers(const Vector &p_headers) { for (int i = 0; i < p_headers.size(); i++) { String sanitized = p_headers[i].strip_edges(); @@ -166,6 +170,7 @@ void HTTPClient::_bind_methods() { ClassDB::bind_method(D_METHOD("set_https_proxy", "host", "port"), &HTTPClient::set_https_proxy); ClassDB::bind_method(D_METHOD("query_string_from_dict", "fields"), &HTTPClient::query_string_from_dict); + ClassDB::bind_static_method("HTTPClient", D_METHOD("query_string_from", "fields"), &HTTPClient::query_string_from); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "blocking_mode_enabled"), "set_blocking_mode", "is_blocking_mode_enabled"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "connection", PROPERTY_HINT_RESOURCE_TYPE, "StreamPeer", PROPERTY_USAGE_NONE), "set_connection", "get_connection"); diff --git a/core/io/http_client.h b/core/io/http_client.h index 9e018182e379..b66ae4496856 100644 --- a/core/io/http_client.h +++ b/core/io/http_client.h @@ -165,6 +165,7 @@ class HTTPClient : public RefCounted { public: static HTTPClient *create(); + static String query_string_from(const Dictionary &p_dict); String query_string_from_dict(const Dictionary &p_dict); Error verify_headers(const Vector &p_headers); diff --git a/doc/classes/HTTPClient.xml b/doc/classes/HTTPClient.xml index 25b6a48283ff..7b45dea0852a 100644 --- a/doc/classes/HTTPClient.xml +++ b/doc/classes/HTTPClient.xml @@ -93,7 +93,7 @@ This needs to be called in order to have any request processed. Check results with [method get_status]. - + @@ -101,12 +101,12 @@ [codeblocks] [gdscript] var fields = {"username": "user", "password": "pass"} - var query_string = http_client.query_string_from_dict(fields) + var query_string = HTTPClient.query_string_from_dict(fields) # Returns "username=user&password=pass" [/gdscript] [csharp] var fields = new Godot.Collections.Dictionary { { "username", "user" }, { "password", "pass" } }; - string queryString = httpClient.QueryStringFromDict(fields); + string queryString = HTTPClient.QueryStringFromDict(fields); // Returns "username=user&password=pass" [/csharp] [/codeblocks] @@ -114,7 +114,7 @@ [codeblocks] [gdscript] var fields = {"single": 123, "not_valued": null, "multiple": [22, 33, 44]} - var query_string = http_client.query_string_from_dict(fields) + var query_string = HTTPClient.query_string_from_dict(fields) # Returns "single=123&not_valued&multiple=22&multiple=33&multiple=44" [/gdscript] [csharp] @@ -124,12 +124,19 @@ { "notValued", default }, { "multiple", new Godot.Collections.Array { 22, 33, 44 } }, }; - string queryString = httpClient.QueryStringFromDict(fields); + string queryString = HTTPClient.QueryStringFromDict(fields); // Returns "single=123&not_valued&multiple=22&multiple=33&multiple=44" [/csharp] [/codeblocks] + + + + + Generates a GET/POST application/x-www-form-urlencoded style query string from a provided dictionary. + + diff --git a/tests/core/io/test_http_client.h b/tests/core/io/test_http_client.h index 96a7735d0359..eb58117120ad 100644 --- a/tests/core/io/test_http_client.h +++ b/tests/core/io/test_http_client.h @@ -43,14 +43,13 @@ TEST_CASE("[HTTPClient] Instantiation") { } TEST_CASE("[HTTPClient] query_string_from_dict") { - Ref client = HTTPClient::create(); Dictionary empty_dict; - String empty_query = client->query_string_from_dict(empty_dict); + String empty_query = HTTPClient::query_string_from(empty_dict); CHECK_MESSAGE(empty_query.is_empty(), "A empty dictionary should return a empty string"); Dictionary dict1; dict1["key"] = "value"; - String single_key = client->query_string_from_dict(dict1); + String single_key = HTTPClient::query_string_from(dict1); CHECK_MESSAGE(single_key == "key=value", "The query should return key=value for every string in the dictionary"); // Check Dictionary with multiple values of different types. @@ -63,7 +62,7 @@ TEST_CASE("[HTTPClient] query_string_from_dict") { values.push_back(3); dict2["key3"] = values; dict2["key4"] = Variant(); - String multiple_keys = client->query_string_from_dict(dict2); + String multiple_keys = HTTPClient::query_string_from(dict2); CHECK_MESSAGE(multiple_keys == "key1=value&key2=123&key3=1&key3=2&key3=3&key4", "The query should return key=value for every string in the dictionary. Pairs should be separated by &, arrays should be have a query for every element, and variants should have empty values"); }