httpHead实现Gzip压缩
时间: 2023-04-12 21:02:11 浏览: 75
可以使用以下代码实现:
local http = require("socket.http")
local ltn12 = require("ltn12")
local zlib = require("zlib")
local url = "http://example.com"
local request_body = "Hello, world!"
local response_body = {}
local request_headers = {
["Content-Type"] = "text/plain",
["Content-Encoding"] = "gzip",
["Content-Length"] = #request_body,
}
local request = {
url = url,
method = "POST",
headers = request_headers,
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body),
}
local ok, code, headers, status = http.request(request)
if ok then
local response_body_string = table.concat(response_body)
local response_body_gzip = zlib.compress(response_body_string)
print("Response body (gzip): " .. response_body_gzip)
else
print("HTTP request failed: " .. code)
end
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)