简体   繁体   中英

Luasec Luasocket https.request with user agent and return body

I want to make a https.request with a custom user-agent using LuaSec. I've tried the method described here:

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

But I don't know how to pass http headers to this function with avoiding to get the response body set to the number 1, as described:

If url is a table , the function returns the same results, except the response's body is replaced by the value 1 .

 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!

If I change the request to

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

I get the respone body, but then I can't set the user agent anymore.

Thanks for your help!

If you specify a table, the first return element is always 1 on success; to receive the actual data you have to also specify a sink , to which the received data is to be stored. For example:

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)

Now you can retrieve the response from the variable response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM