diff options
| author | Rémi Verschelde <rverschelde@gmail.com> | 2019-12-30 16:34:09 +0100 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-30 16:34:09 +0100 | 
| commit | d595a5e9c3f46fd8c09a8deef4b7bca2959c457e (patch) | |
| tree | a881a5b46bf90a0f99c036b513d67da77f96ab22 | |
| parent | 1da9a60cd8ed7ac5b40bf11c3fe6853f3aa04255 (diff) | |
| parent | c719cea11675e099058c6e05cc0a1b4c3ebfec76 (diff) | |
Merge pull request #34593 from Calinou/doc-httprequest-json-example
Add an example of using HTTPRequest to contact a REST API
| -rw-r--r-- | doc/classes/HTTPRequest.xml | 21 | 
1 files changed, 21 insertions, 0 deletions
| diff --git a/doc/classes/HTTPRequest.xml b/doc/classes/HTTPRequest.xml index 2fed423a32..748ed504c3 100644 --- a/doc/classes/HTTPRequest.xml +++ b/doc/classes/HTTPRequest.xml @@ -6,6 +6,27 @@  	<description>  		A node with the ability to send HTTP requests. Uses [HTTPClient] internally.  		Can be used to make HTTP requests, i.e. download or upload files or web content via HTTP. +		[b]Example of contacting a REST API and printing one of its returned fields:[/b] +		[codeblock] +		func _ready(): +		    # Create an HTTP request node and connect its completion signal. +		    var http_request = HTTPRequest.new() +		    add_child(http_request) +		    http_request.connect("request_completed", self, "_http_request_completed") + +		    # Perform the HTTP request. The URL below returns some JSON as of writing. +		    var error = http_request.request("https://httpbin.org/get") +		    if error != OK: +		        push_error("An error occurred in the HTTP request.") + + +		# Called when the HTTP request is completed. +		func _http_request_completed(result, response_code, headers, body): +		    var response = parse_json(body.get_string_from_utf8()) + +		    # Will print the user agent string used by the HTTPRequest node (as recognized by httpbin.org). +		    print(response.headers["User-Agent"]) +		[/codeblock]  		[b]Example of loading and displaying an image using HTTPRequest:[/b]  		[codeblock]  		func _ready(): |