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
Defold HTTP requests also support the HEAD, DELETE and PUT methods.
47
+
Defold HTTP requests also support the HEAD, DELETE and PUT methods. The CONNECT method is also supported (see section about proxy connections).
48
+
49
+
50
+
### How to work with the HTTP response
51
+
52
+
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`:
53
+
54
+
```lua
55
+
56
+
localfunctionhandle_response(self, id, response)
57
+
-- check the response status code. Common response codes:
58
+
-- 200 OK - the request completed successfully
59
+
-- 301 Moved permanently - the requested data has moved, see redirect header
60
+
-- 307 Temporary redirect - same as above
61
+
-- 208 Permanent redirect - same as above
62
+
-- 400 Bad Request - the request was malformed
63
+
-- 401 Unauthorized - the client must authenticate itself
64
+
-- 404 Not Found - the server cannot find the information
-- this can be anything from plain text, json encoded data or binary data
69
+
print(response.response)
70
+
json.decode(response.response)
71
+
sys.save(..., response.response)
72
+
end
73
+
end
74
+
```
75
+
76
+
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:
77
+
78
+
```lua
79
+
-- in this example we download myimage.png and write it directly to a file on disk
80
+
81
+
localoptions= {
82
+
path=sys.get_save_file("mygame", "myimage.png")
83
+
}
84
+
85
+
localfunctionhandle_response(self, id, response)
86
+
ifresponse.status==200then
87
+
print("File was successfully written to:", response.path)
88
+
print("File size:", response.document_size)
89
+
else
90
+
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).
98
+
99
+
100
+
### HTTP Proxy
101
+
102
+
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:
103
+
104
+
105
+
```lua
106
+
-- connect to www.defold.com via localhost proxy on port 8888
0 commit comments