diff options
| author | Pedro J. Estébanez | 2018-03-17 13:09:26 +0100 |
|---|---|---|
| committer | Pedro J. Estébanez | 2018-04-12 21:20:10 +0200 |
| commit | 46101928bdf2022f0da41d16d33451c3d868901b (patch) | |
| tree | 384479128f0f19d3bfb2dff88830a18c0a0f1d04 | |
| parent | b6f30f1b5b525b9c3b187e16a5dd170e7f16f8b5 (diff) | |
| download | godot-46101928bdf2022f0da41d16d33451c3d868901b.tar.gz godot-46101928bdf2022f0da41d16d33451c3d868901b.tar.zst godot-46101928bdf2022f0da41d16d33451c3d868901b.zip | |
Enhance HTTPClient.query_string_from_dict()
(cherry picked from commit 8d8e9d54c859625277c7de977b361165c09b06b1)
| -rw-r--r-- | core/io/http_client.cpp | 22 | ||||
| -rw-r--r-- | doc/classes/HTTPClient.xml | 6 |
2 files changed, 27 insertions, 1 deletions
diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 4d72f744e..9e301ccac 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -618,7 +618,27 @@ String HTTPClient::query_string_from_dict(const Dictionary &p_dict) { String query = ""; Array keys = p_dict.keys(); for (int i = 0; i < keys.size(); ++i) { - query += "&" + String(keys[i]).http_escape() + "=" + String(p_dict[keys[i]]).http_escape(); + String encoded_key = String(keys[i]).http_escape(); + Variant value = p_dict[keys[i]]; + switch (value.get_type()) { + case Variant::ARRAY: { + // Repeat the key with every values + Array values = value; + for (int j = 0; j < values.size(); ++j) { + query += "&" + encoded_key + "=" + String(values[j]).http_escape(); + } + break; + } + case Variant::NIL: { + // Add the key with no value + query += "&" + encoded_key; + break; + } + default: { + // Add the key-value pair + query += "&" + encoded_key + "=" + String(value).http_escape(); + } + } } query.erase(0, 1); return query; diff --git a/doc/classes/HTTPClient.xml b/doc/classes/HTTPClient.xml index 5627f585e..3f0668ba0 100644 --- a/doc/classes/HTTPClient.xml +++ b/doc/classes/HTTPClient.xml @@ -111,6 +111,12 @@ String queryString = httpClient.query_string_from_dict(fields) returns:= "username=user&password=pass" [/codeblock] + Furthermore, if a key has a null value, only the key itself is added, without equal sign and value. If the value is an array, for each value in it a pair with the same key is added. + [codeblock] + var fields = {"single": 123, "not_valued": null, "multiple": [22, 33, 44]} + String queryString = httpClient.query_string_from_dict(fields) + returns:= "single=123&not_valued&multiple=22&multiple=33&multiple=44" + [/codeblock] </description> </method> <method name="read_response_body_chunk"> |
