You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
Updated http_client (markdown)
@@ -11,56 +11,83 @@ It will connect and fetch a website.
|
|||||||
```python
|
```python
|
||||||
extends SceneMainLoop
|
extends SceneMainLoop
|
||||||
|
|
||||||
|
# HTTPClient demo
|
||||||
|
# This simple class can do HTTP requests, it will not block but it needs to be polled
|
||||||
|
|
||||||
func _init():
|
func _init():
|
||||||
|
|
||||||
var err=0
|
var err=0
|
||||||
var http = HTTPClient.new()
|
var http = HTTPClient.new() # Create the Client
|
||||||
|
|
||||||
|
var err = http.connect("www.php.net",80) # Connect to host/port
|
||||||
|
assert(err==OK) # Make sure connection was OK
|
||||||
|
|
||||||
|
|
||||||
var err = http.connect("www.php.net",80)
|
|
||||||
assert(err==OK)
|
|
||||||
while( http.get_status()==HTTPClient.STATUS_CONNECTING or http.get_status()==HTTPClient.STATUS_RESOLVING):
|
while( http.get_status()==HTTPClient.STATUS_CONNECTING or http.get_status()==HTTPClient.STATUS_RESOLVING):
|
||||||
|
#Wait until resolved and connected
|
||||||
http.poll()
|
http.poll()
|
||||||
print("Connecting..")
|
print("Connecting..")
|
||||||
OS.delay_msec(500)
|
OS.delay_msec(500)
|
||||||
assert( http.get_status() == HTTPClient.STATUS_CONNECTED )
|
|
||||||
print("Connected! Requesting INDEX")
|
assert( http.get_status() == HTTPClient.STATUS_CONNECTED ) # Could not connect
|
||||||
|
|
||||||
|
# Some headers
|
||||||
|
|
||||||
var headers=[
|
var headers=[
|
||||||
"User-Agent: Pirulo/1.0 (Godot)",
|
"User-Agent: Pirulo/1.0 (Godot)",
|
||||||
"Accept: */*"
|
"Accept: */*"
|
||||||
]
|
]
|
||||||
|
|
||||||
err = http.request(HTTPClient.METHOD_GET,"/ChangeLog-5.php",headers)
|
err = http.request(HTTPClient.METHOD_GET,"/ChangeLog-5.php",headers) # Request a page from the site (this one was chunked..)
|
||||||
assert( err == OK )
|
|
||||||
|
assert( err == OK ) # Make sure all is OK
|
||||||
|
|
||||||
while (http.get_status() == HTTPClient.STATUS_REQUESTING):
|
while (http.get_status() == HTTPClient.STATUS_REQUESTING):
|
||||||
|
# Keep polling until the request is going on
|
||||||
http.poll()
|
http.poll()
|
||||||
print("Requesting..")
|
print("Requesting..")
|
||||||
OS.delay_msec(500)
|
OS.delay_msec(500)
|
||||||
assert( http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED )
|
|
||||||
|
|
||||||
print("has response? ",http.has_response())
|
|
||||||
|
assert( http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED ) # Make sure request finished well.
|
||||||
|
|
||||||
|
print("response? ",http.has_response()) # Site might not have a response.
|
||||||
|
|
||||||
|
|
||||||
if (http.has_response()):
|
if (http.has_response()):
|
||||||
var headers = http.get_response_headers_as_dictionary()
|
#If there is a response..
|
||||||
print("code: ",http.get_response_code())
|
|
||||||
print("**headers:\n",headers)
|
var headers = http.get_response_headers_as_dictionary() # Get response headers
|
||||||
# ok let's get the body!
|
print("code: ",http.get_response_code()) # Show response code
|
||||||
|
print("**headers:\n",headers) # Show headers
|
||||||
|
|
||||||
|
#Getting the HTTP Body
|
||||||
|
|
||||||
if (http.is_response_chunked()):
|
if (http.is_response_chunked()):
|
||||||
|
#Does it use chunks?
|
||||||
print("Respose is Chunked!")
|
print("Respose is Chunked!")
|
||||||
else:
|
else:
|
||||||
|
#Or just plain Content-Length
|
||||||
var bl = http.get_response_body_length()
|
var bl = http.get_response_body_length()
|
||||||
print("Response Length: ",bl)
|
print("Response Length: ",bl)
|
||||||
|
|
||||||
var rb = RawArray()
|
#This method works for both anyway
|
||||||
|
|
||||||
|
var rb = RawArray() #array that will hold the data
|
||||||
|
|
||||||
while(http.get_status()==HTTPClient.STATUS_BODY):
|
while(http.get_status()==HTTPClient.STATUS_BODY):
|
||||||
|
#While there is body left to be read
|
||||||
http.poll()
|
http.poll()
|
||||||
var chunk = http.read_response_body_chunk()
|
var chunk = http.read_response_body_chunk() # Get a chunk
|
||||||
if (chunk.size()==0):
|
if (chunk.size()==0):
|
||||||
#wait for a bit
|
#got nothing, wait for buffers to fill a bit
|
||||||
OS.delay_usec(1000)
|
OS.delay_usec(1000)
|
||||||
else:
|
else:
|
||||||
rb = rb + chunk
|
rb = rb + chunk # append to read bufer
|
||||||
|
|
||||||
|
|
||||||
|
#done!
|
||||||
|
|
||||||
print("bytes got: ",rb.size())
|
print("bytes got: ",rb.size())
|
||||||
var text = rb.get_string_from_ascii()
|
var text = rb.get_string_from_ascii()
|
||||||
print("Text: ",text)
|
print("Text: ",text)
|
||||||
@@ -69,5 +96,4 @@ func _init():
|
|||||||
quit()
|
quit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user