瀏覽代碼

HTTP networking docs

mhilbrunner 7 年之前
父節點
當前提交
c2192d60a0

+ 4 - 0
tutorials/networking/http_client_class.rst

@@ -3,6 +3,10 @@
 HTTP client class
 HTTP client class
 =================
 =================
 
 
+:ref:`HTTPClient <class_HTTPClient>` provides low-level access to HTTP communication.
+For a more higher-level interface, you may want to take a look at :ref:`HTTPRequest <class_HTTPRequest>` first,
+which has a tutorial available :ref:`here <doc_http_request_class>`.
+
 Here's an example of using the :ref:`HTTPClient <class_HTTPClient>`
 Here's an example of using the :ref:`HTTPClient <class_HTTPClient>`
 class. It's just a script, so it can be run by executing:
 class. It's just a script, so it can be run by executing:
 
 

+ 44 - 0
tutorials/networking/http_request_class.rst

@@ -0,0 +1,44 @@
+.. _doc_http_request_class:
+
+Making HTTP requests
+====================
+
+The :ref:`HTTPRequest <class_HTTPRequest>` node is the easiest way to make HTTP requests in Godot.
+It is backed by the more low-level :ref:`HTTPClient <class_HTTPClient>`, for which a tutorial is available :ref:`here <doc_http_client_class>`.
+
+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 <class_JSON>` and :ref:`JSONParseResult <class_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 <class_HTTPRequest>` for more.

+ 1 - 0
tutorials/networking/index.rst

@@ -6,6 +6,7 @@ Networking
    :name: toc-learn-features-networking
    :name: toc-learn-features-networking
 
 
    high_level_multiplayer
    high_level_multiplayer
+   http_request_class
    http_client_class
    http_client_class
    ssl_certificates
    ssl_certificates
 
 

+ 0 - 36
tutorials/networking/making_calls_to_a_rest_api.rst

@@ -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.