I’m trying to interact with the Sipgate api (https://developer.sipgate.io/ ) using Lua, and an example they provide for authentication uses Curl, which works. (Example is not my b64 user/pass)
The URL you are accessing responds with a redirect request which the LuaSocket library doesn’t automatically handle. You would have to manually code another (or possibly several) request to the new URL specified in the response header.
No, that should be it, although it may return another redirection. So if you write a loop, make sure that it has a limited number of attempts, to avoid circular redirections (happens, sometimes.)
Will do, although my first hurdle is extracting that redirect url from the ‘headers’ table .
This is an area that drives me mad, as I constantly struggle to get the right structure - e.g. none of the following work. I know the key is ‘location’ but I can’t extract the value ?
for k,v in pairs(headers) do
-- print(k,v)
-- print(v[1])
-- print(['location'].v)
-- print(v.location)
-- print(v'location')
-- print(v['location'])
end
Please could someone let me know the right format?
Thanks again @ElCid, ok, so next I need to create something that handles the redirect.
I have the following code so far, which is looking to see if a redirect is returned within the headers. As one does in this case, how best should I handle that, is it best to create another function, and have the first function call that one ?
local https = require "ssl.https"
local mime = require "mime"
local http = require "socket.http"
local ltn12 = require "ltn12"
local username = "user"
local password = "pass"
local function httprequester(url)
local ok, statusCode, headers, statusText = http.request {
method = "GET",
url = url ,
headers = {
["Accept"] = "application/json",
["Authorization"] = "Basic " .. (mime.b64(username ..":" .. username)),
},
sink = ltn12.sink.table(respbody),
}
if headers.location ~= nil then
local firstredirect = tostring(headers.location)
print ("first redirect :", firstredirect)
local result,b,c,h = http.request{
url = headers.location,
headers = header,method="GET" }
local secondredirect = tostring(c.location)
print ("second redirect :", secondredirect)
--[[for key,val in pairs(c) do
print(key,val) ]]--
end
end
--[[
--debug
print("enc64", mime.b64(username ..":" .. password))
print("ok\t", ok);
print("statusCode", statusCode)
print("statusText", statusText)
print("headers:", headers)
print("sink", sink)
]]
return redirect
end
print(httprequester("https://api.sipgate.com/v2/account"))
```
It could be that the SSL options are the source of the problem. On Vera the library is old and has some outdated options by default. Does this little wrapper help?
-- Need wrapper for Vera to set protocol correctly. Sadly tls1.2 is not supported on the Lite if remote site now requireds that.
local function HttpsWGet(strURL, timeout)
local result = {}
local to = timeout or 60
http.TIMEOUT = to
local bdy,cde,hdrs,stts = https.request{
url = strURL,
method = "GET",
protocol = "any",
options = {"all", "no_sslv2", "no_sslv3"},
verify = "none",
sink=ltn12.sink.table(result)
}
-- Mimick luup.inet.get return values
if bdy == 1 then bdy = 0 else bdy = 1 end
return bdy,table.concat(result),cde
end
curl -X GET "https://api.sipgate.com/v2/account" -H "accept: application/json" -H "Authorization: Basic Sm9objp0b3BzZWNyZXQ=="curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
Adding the —insecure option to the curl request and it worked…