Defold can make normal HTTP requests using the http.request()
function.
This is the most basic request to get some data from the server. Example:
local function handle_response(self, id, response)
print(response.status, response.response)
end
http.request("https://www.defold.com", "GET", handle_response)
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.
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
.
When sending data, like a score or some authentication data, to a server it is typically done using a POST requests:
local function handle_response(self, id, response)
print(response.status, response.response)
end
local headers = {
["Content-Type"] = "application/x-www-form-urlencoded"
}
local body = "foo=bar"
http.request("https://httpbin.org/post", "POST", handle_response, headers, body)
Defold HTTP requests also support the HEAD, DELETE and PUT methods.
Refer to the API reference to learn more.
An alternative HTTP request implementation can be found in the TinyHTTP extension.
Did you spot an error or do you have a suggestion? Please let us know on GitHub!
GITHUB