api - socket http lua 设置超时
问题描述
我正在尝试创建一个可以使用 http 套接字 lua 调用 REST 的函数。我试图以这种方式设置超时。但是,当我运行这个函数时,超时没有运行。我应该如何设置超时?
local http = require "socket.http"
local socket = require "socket"
local respbody = {}
http.request {
method = req_method,
url = req_url,
source = ltn12.source.string(req_body),
headers =
{
["Content-Type"] = req_content_type,
["content-length"] = string.len(req_body),
["Host"] = host,
},
sink = ltn12.sink.table(respbody),
create = function()
local req_sock = socket.tcp()
req_sock:settimeout(3, 't')
return req_sock
end,
}
解决方案
如果我按照我的要求实现这个例子,会是这样吗?cmiiw
local http_client = require "http.client"
local req_body = "key1=value1&key2=value2"
local myconnection = http_client.connect {
method = "POST";
url = "myrestserver.domain.com/api/example";
host = "myrestserver.domain.com";
source = req_body
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["content-length"] = string.len(req_body),
},
timeout = 2;
}
推荐阅读
- c# - 如何在运行时导入 Winforms 设置文件
- asp.net-mvc - 如何从 ASP.NET Core 上的控制器调用集线器中的方法?
- python - 如何连接具有相同键和互补值的两行
- r - 如何将重复项合并到 R 中的每列一个单元格中
- powerbi - DAX - 按年份筛选运行总计
- c# - C#中是否有所有委托的基类
- angular - 如何在完成由单元格编辑触发的异步方法之前和之后更改 Ag Grid 中的单元格样式?
- javascript - JavaScript ES6 模块 MIME 类型
- python - AttributeError:“unicode”对象没有属性“fromstring”。如何解决这个问题?
- mysql - 动态连接 MySQL 表