This translation is community contributed and may not be up to date. We only maintain the English version of the documentation. Read this manual in English
Defold可以使用http.request()
函数进行常规HTTP请求。
这是从服务器获取一些数据的最基本请求。示例:
local function handle_response(self, id, response)
print(response.status, response.response)
end
http.request("https://www.defold.com", "GET", handle_response)
这将向https://www.defold.com发出HTTP GET请求。该函数是异步的,在发出请求时不会阻塞。一旦请求完成并收到服务器响应,它将调用提供的回调函数。回调函数将接收完整的服务器响应,包括状态码和响应头。
HTTP请求会自动缓存在客户端以提高网络性能。缓存的文件存储在操作系统特定的应用程序支持路径中,位于名为defold/http-cache
的文件夹中。您通常不需要关心HTTP缓存,但如果在开发过程中需要清除缓存,可以手动删除包含缓存文件的文件夹。在macOS上,此文件夹位于%HOME%/Library/Application Support/Defold/http-cache/
,在Windows上位于%APP_DATA%/defold/http-cache
。
当向服务器发送数据,如分数或某些认证数据时,通常使用POST请求:
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请求还支持HEAD、DELETE和PUT方法。
请参考API参考了解更多。
替代的HTTP请求实现可以在TinyHTTP扩展中找到。
Did you spot an error or do you have a suggestion? Please let us know on GitHub!
GITHUB