From c2192d60a0285268d2fcac155884553236e3aa8b Mon Sep 17 00:00:00 2001 From: mhilbrunner Date: Fri, 2 Mar 2018 11:53:00 +0100 Subject: [PATCH] HTTP networking docs --- tutorials/networking/http_client_class.rst | 4 ++ tutorials/networking/http_request_class.rst | 44 +++++++++++++++++++ tutorials/networking/index.rst | 1 + .../networking/making_calls_to_a_rest_api.rst | 36 --------------- 4 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 tutorials/networking/http_request_class.rst delete mode 100644 tutorials/networking/making_calls_to_a_rest_api.rst diff --git a/tutorials/networking/http_client_class.rst b/tutorials/networking/http_client_class.rst index 552984f40..118c346e4 100644 --- a/tutorials/networking/http_client_class.rst +++ b/tutorials/networking/http_client_class.rst @@ -3,6 +3,10 @@ HTTP client class ================= +:ref:`HTTPClient ` provides low-level access to HTTP communication. +For a more higher-level interface, you may want to take a look at :ref:`HTTPRequest ` first, +which has a tutorial available :ref:`here `. + Here's an example of using the :ref:`HTTPClient ` class. It's just a script, so it can be run by executing: diff --git a/tutorials/networking/http_request_class.rst b/tutorials/networking/http_request_class.rst new file mode 100644 index 000000000..d49b6e574 --- /dev/null +++ b/tutorials/networking/http_request_class.rst @@ -0,0 +1,44 @@ +.. _doc_http_request_class: + +Making HTTP requests +==================== + +The :ref:`HTTPRequest ` node is the easiest way to make HTTP requests in Godot. +It is backed by the more low-level :ref:`HTTPClient `, for which a tutorial is available :ref:`here `. + +For the sake of example, we will create a simple UI with a button, that when pressed will start the HTTP request to the specified URL. + +Preparing scene +--------------- + +Create a new empty scene, add a CanvasLayer as the root node and add an script to it. Then add two child nodes to it: a Button and an HTTPRequest node. You will need to connect the following signals to the CanvasLayer script: + +- Button.pressed: When the button is pressed, we will start the request. +- HTTPRequest.request_completed: When the request is completed, we will get the requested data as an argument. + +.. image:: img/rest_api_scene.png + +Scripting +--------- + +Below is all the code we need to make it work. The URL points to an online API mocker; it returns a pre-defined JSON string, which we will then parse to get access to the data. + +:: + + extends CanvasLayer + + func _ready(): + pass + + func _on_Button_pressed(): + $HTTPRequest.request("http://www.mocky.io/v2/5185415ba171ea3a00704eed") + + func _on_HTTPRequest_request_completed( result, response_code, headers, body ): + var json = JSON.parse(body.get_string_from_utf8()) + print(json.result) + +With this, you should see ``(hello:world)`` printed on the console; hello being a key, and world being a value, both of them strings. + +For more information on parsing JSON, see the class references for :ref:`JSON ` and :ref:`JSONParseResult `. + +Note that you may want to check whether the ``result`` equals ``RESULT_SUCCESS`` and whether a JSON parsing error occurred, see the JSON class reference and :ref:`HTTPRequest ` for more. diff --git a/tutorials/networking/index.rst b/tutorials/networking/index.rst index 9ca5fc243..e9e3e79fe 100644 --- a/tutorials/networking/index.rst +++ b/tutorials/networking/index.rst @@ -6,6 +6,7 @@ Networking :name: toc-learn-features-networking high_level_multiplayer + http_request_class http_client_class ssl_certificates diff --git a/tutorials/networking/making_calls_to_a_rest_api.rst b/tutorials/networking/making_calls_to_a_rest_api.rst deleted file mode 100644 index 307728cbd..000000000 --- a/tutorials/networking/making_calls_to_a_rest_api.rst +++ /dev/null @@ -1,36 +0,0 @@ -.. _doc_making_calls_to_a_rest_api: - -Making calls to a REST API -========================== - -For the sake of example, we will create a simple UI with a button, that when pressed, it will start the request to the specified URL. To make the request, we will make use of the HTTPRequest node. - -Preparing scene ---------------- - -Create a new empty scene, add a CanvasLayer as the root node, and add an script to it. Then add two child nodes to it; a Button, and an HTTPRequest node. You will need to connect the following signals to the CanvasLayer script. - -- Button.pressed: When the button is pressed, we will start the request. -- HTTPRequest.request_completed: When the request is completed, we will get the requested data as an argument. - -.. image:: img/rest_api_scene.png - -Scripting ---------- -This is all the code we need to make it work. The URL, is an online RestAPI mocker; it returns a json string, which we will then parse to get access to the data. - -:: - - extends CanvasLayer - - func _ready(): - pass - - func _on_Button_pressed(): - $HTTPRequest.request("http://www.mocky.io/v2/5185415ba171ea3a00704eed") - - func _on_HTTPRequest_request_completed( result, response_code, headers, body ): - var json = JSON.parse(body.get_string_from_utf8()) - print(json.result) - -With this, you should see ``(hello:world)`` printed on the console; hello being a key, and world being a value, both of them strings.