mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-03 05:48:42 +03:00
Hello Word to make calls to a RestAPI.
This commit is contained in:
BIN
tutorials/networking/img/rest_api_scene.png
Normal file
BIN
tutorials/networking/img/rest_api_scene.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.2 KiB |
36
tutorials/networking/making_calls_to_a_rest_api.rst
Normal file
36
tutorials/networking/making_calls_to_a_rest_api.rst
Normal file
@@ -0,0 +1,36 @@
|
||||
.. _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.
|
||||
Reference in New Issue
Block a user