【问题标题】:Luasec Luasocket https.request with user agent and return bodyLuasec Luasocket https.request 与用户代理和返回正文
【发布时间】:2018-08-29 03:03:02
【问题描述】:

我想使用 LuaSec 使用自定义用户代理创建 https.request。 我已经尝试过这里描述的方法:

https://github.com/brunoos/luasec/wiki/LuaSec-0.4#httpsrequesturl---body

但我不知道如何将 http 标头传递给此函数,同时避免将响应正文设置为数字 1,如下所述:

如果 urltable,则该函数返回相同的结果,只是响应的正文被替换为值 1。 p>

 local headers = {
    ["user-agent"] = "My User Agent",
}

local r, c, h, s = _https.request {
    url = url,
    method = method,
    headers = headers,
}
-- r is 1, no the response body!

如果我将请求更改为

_https.request("https://my-site-goes-here.com/")

我得到了响应正文,但是我不能再设置用户代理了。

感谢您的帮助!

【问题讨论】:

    标签: lua get httprequest luasocket luasec


    【解决方案1】:

    如果你指定一个表格,第一个返回元素是always 1 成功;要接收实际数据,您还必须指定一个接收器,接收的数据将存储到该接收器。例如:

    local chunks = {}
    local r, c, h, s = _https.request {
        url = url,
        method = method,
        headers = headers,
        sink = ltn12.sink.table(chunks)
    }
    local response = table.concat(chunks)
    

    现在您可以从变量 response 检索响应

    【讨论】:

    • 在阅读文档时,我找不到任何关于该接收器属性的重要性的信息。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 2023-04-02
    • 2019-10-29
    • 2021-02-08
    • 2022-06-11
    • 1970-01-01
    • 2010-11-06
    相关资源
    最近更新 更多