You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This will make an HTTP GET request to https://www.defold.com. The function is asynchronous and will not block while making the request. Once the request has been made and a server has sent a response it will invoke/call the provided callback function. The callback function will receive the full server response, including status code and response headers.
22
+
This will make an HTTP GET request to https://www.defold.com. The function is asynchronous and will not block while making the request. Once the request has been made and a server has sent a response it will invoke/call the provided callback function. The callback function will receive the full server response, including status code and response headers. See below for additional information about how to work with the response table.
23
23
24
24
::: sidenote
25
25
HTTP requests are automatically cached in the client to improve network performance. The cached files are stored in an OS specific application support path in a folder named `defold/http-cache`. You usually don't have to care about the HTTP cache but if you need to clear the cache during development you can manually delete the folder containing the cached files. On macOS this folder is located in `%HOME%/Library/Application Support/Defold/http-cache/` and on Windows in `%APP_DATA%/defold/http-cache`.
@@ -29,21 +29,143 @@ HTTP requests are automatically cached in the client to improve network performa
29
29
30
30
When sending data, like a score or some authentication data, to a server it is typically done using a POST requests:
Defold HTTP requests also support the HEAD, DELETE and PUT methods. The CONNECT method is also supported (see section about proxy connections below).
45
+
46
+
### How to work with the HTTP response
47
+
48
+
The `response` table returned in the callback contains all of the information necessary to implement granular reponse handling. Two of the key fields are `status` and `response`:
49
+
50
+
```lua
51
+
52
+
localfunctionhandle_response(self, id, response)
53
+
-- check the response status code. Common response codes:
54
+
-- 200 OK - the request completed successfully
55
+
-- 301 Moved permanently - the requested data has moved, see redirect header
56
+
-- 307 Temporary redirect - same as above
57
+
-- 208 Permanent redirect - same as above
58
+
-- 400 Bad Request - the request was malformed
59
+
-- 401 Unauthorized - the client must authenticate itself
60
+
-- 404 Not Found - the server cannot find the information
-- this can be anything from plain text, json encoded data or binary data
65
+
print(response.response)
66
+
json.decode(response.response)
67
+
sys.save(..., response.response)
68
+
end
69
+
end
70
+
```
71
+
72
+
When the response contains a large blob of binary data such as an image or a music track it might make sense to write the data to a file instead of loading it into memory:
73
+
74
+
```lua
75
+
-- in this example we download myimage.png and write it directly to a file on disk
76
+
77
+
localoptions= {
78
+
path=sys.get_save_file("mygame", "myimage.png")
79
+
}
80
+
81
+
localfunctionhandle_response(self, id, response)
82
+
ifresponse.status==200then
83
+
print("File was successfully written to:", response.path)
84
+
print("File size:", response.document_size)
85
+
print("File path:", response.path)
86
+
else
87
+
print("File was not written to disk:", response.error)
Another use-case for loading large amounts of data over the network is sound streaming, when "chunks" of sound data are loaded from a URL and fed into a sound resource. A complete example can be found in the [Sound Streaming manual](/sound-streaming#sound-streaming).
95
+
96
+
97
+
### Request headers
98
+
99
+
It is possible to set additional headers when sending a request. This can for instance be used to set an authorization header or content type to tell the server which format the
It is sometimes desirable to send a request through a proxy server. This can be done by specifying a proxy server to use when connecting to the destination server. When a proxy is used the connection to the destination server is established using an a HTTP tunnel through the proxy. The HTTP tunnel is established using the CONNECT HTTP method. Example:
154
+
155
+
156
+
```lua
157
+
-- connect to www.defold.com via localhost proxy on port 8888
0 commit comments