Skip to content

Commit bc5d8bd

Browse files
committed
Update http-requests.md
1 parent 238bb0b commit bc5d8bd

1 file changed

Lines changed: 72 additions & 1 deletion

File tree

docs/en/manuals/http-requests.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,80 @@ local body = "foo=bar"
4141
http.request("https://httpbin.org/post", "POST", handle_response, headers, body)
4242
```
4343

44+
4445
### Other HTTP methods
4546

46-
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+
local function handle_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
65+
-- https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status
66+
if response.status == 200 then
67+
-- the response data
68+
-- 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+
local options = {
82+
path = sys.get_save_file("mygame", "myimage.png")
83+
}
84+
85+
local function handle_response(self, id, response)
86+
if response.status == 200 then
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)
91+
end
92+
end
93+
94+
http.request("https://www.foobar.com/myimage.png", "GET", handle_response)
95+
```
96+
97+
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
107+
local url = "https://www.defold.com:443"
108+
local method = "GET"
109+
local headers = {}
110+
local post_data = nil
111+
local options = {
112+
proxy = "https://127.0.0.1:8888"
113+
}
114+
http.request(url, method, function(self, id, response)
115+
pprint(response)
116+
end, headers, post_data, options)
117+
```
47118

48119
### API Reference
49120

0 commit comments

Comments
 (0)