Oxy Synth
Oxy Synth

Reputation: 154

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!

Upvotes: 1

Views: 2281

Answers (1)

Ctx
Ctx

Reputation: 18410

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

Upvotes: 2

Related Questions