Skip to content

Commit 17b64f6

Browse files
committed
Review fixes
1 parent bc5d8bd commit 17b64f6

1 file changed

Lines changed: 61 additions & 10 deletions

File tree

docs/en/manuals/http-requests.md

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ Defold can make normal HTTP requests using the `http.request()` function.
1111

1212
This is the most basic request to get some data from the server. Example:
1313

14-
```Lua
14+
```lua
1515
local function handle_response(self, id, response)
1616
print(response.status, response.response)
1717
end
1818

1919
http.request("https://www.defold.com", "GET", handle_response)
2020
```
2121

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.
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.
2323

2424
::: sidenote
2525
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,23 +29,19 @@ HTTP requests are automatically cached in the client to improve network performa
2929

3030
When sending data, like a score or some authentication data, to a server it is typically done using a POST requests:
3131

32-
```Lua
32+
```lua
3333
local function handle_response(self, id, response)
3434
print(response.status, response.response)
3535
end
3636

37-
local headers = {
38-
["Content-Type"] = "application/x-www-form-urlencoded"
39-
}
40-
local body = "foo=bar"
41-
http.request("https://httpbin.org/post", "POST", handle_response, headers, body)
37+
local body = "12345"
38+
http.request("https://www.myserver.com/score", "POST", handle_response, nil, body)
4239
```
4340

4441

4542
### Other HTTP methods
4643

47-
Defold HTTP requests also support the HEAD, DELETE and PUT methods. The CONNECT method is also supported (see section about proxy connections).
48-
44+
Defold HTTP requests also support the HEAD, DELETE and PUT methods. The CONNECT method is also supported (see section about proxy connections below).
4945

5046
### How to work with the HTTP response
5147

@@ -86,6 +82,7 @@ local function handle_response(self, id, response)
8682
if response.status == 200 then
8783
print("File was successfully written to:", response.path)
8884
print("File size:", response.document_size)
85+
print("File path:", response.path)
8986
else
9087
print("File was not written to disk:", response.error)
9188
end
@@ -97,6 +94,60 @@ http.request("https://www.foobar.com/myimage.png", "GET", handle_response)
9794
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).
9895

9996

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
100+
101+
```Lua
102+
local function handle_response(self, id, response)
103+
print(response.status, response.response)
104+
end
105+
106+
-- send some form data
107+
local headers = {
108+
["Content-Type"] = "application/x-www-form-urlencoded"
109+
}
110+
local body = "key1=value1&key2=value2"
111+
http.request("https://www.myserver.com/post", "POST", handle_response, headers, body)
112+
113+
-- send some json encoded data
114+
local headers = {
115+
["Content-Type"] = "application/json"
116+
}
117+
local body = json.encode({ key1 = value1, key2 = value2 })
118+
http.request("https://www.myserver.com/post", "POST", handle_response, headers, body)
119+
120+
-- request some data which requires authorization to access
121+
local token = ... -- generate an access token (JWT, OAuth etc)
122+
local headers = {
123+
["Authorization"] = "Bearer " .. token
124+
}
125+
http.request("https://www.myserver.com/content", "GET", handle_response, headers)
126+
```
127+
128+
Defold will automatically set a couple of request headers:
129+
130+
* `If-None-Match: <etag>` will be set with the ETag of any previously cached response.
131+
* `Transfer-Encoding: chunked` will be set if the request body is larger than 16384 bytes.
132+
* `Content-Length` will be set with the size of the request body (unless the request is chunked).
133+
* `Range: bytes=<from>-<to>` will be set if requesting a partial response, for instance when [streaming sounds](/sound-streaming#sound-streaming).
134+
135+
136+
### Response headers
137+
138+
The server response may contain one or more response headers. These are available on the `response` table:
139+
140+
```lua
141+
local function handle_response(self, id, response)
142+
for header,value in pairs(response.headers) do
143+
print(header, value)
144+
end
145+
end
146+
147+
http.request("https://www.defold.com", "GET", handle_response)
148+
```
149+
150+
100151
### HTTP Proxy
101152

102153
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:

0 commit comments

Comments
 (0)