Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HTTPS page content with Lua

I'm trying to access the contents of a webpage from my lua code. The following code works for non-HTTPS pages

local http=require("socket.http")

body,c,l,h = http.request("http://www.example.com:443")

print("status line",l)
print("body",body)

But on HTTPS pages, i get the following error.

Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.

Now i did my research, some people recommends using Luasec, but i couldn't get it to work, no matter how much i tried. Also Luasec is a bit more complicated of a library than what i'm looking for. The page i'm trying to fetch only contains a json object as follows:

{
  "value" : "false",
  "timestamp" : "2017-03-06T14:40:40Z"
}
like image 719
John Boga Avatar asked Mar 06 '17 15:03

John Boga


2 Answers

I have couple of luasec examples in my blog post; assuming you have luasec installed, something as simple as the following should work:

require("socket")
local https = require("ssl.https")
local body, code, headers, status = https.request("https://www.google.com")
print(status)

Sending http requests to port 443 (without using luasec) is not going to work as the http library is not aware of any handshake and encryption steps that need to happen.

If you have specific errors, you should describe what they are, but the above should work.

like image 134
Paul Kulchenko Avatar answered Nov 17 '22 13:11

Paul Kulchenko


try this code:

local https = require('ssl.https')
https.TIMEOUT= 10 
local link = 'http://www.example.com'
local resp = {}
local body, code, headers = https.request{
                                url = link,
                                headers = { ['Connection'] = 'close' },        
                                sink = ltn12.sink.table(resp)
                                 }   
if code~=200 then 
    print("Error: ".. (code or '') ) 
    return 
end
print("Status:", body and "OK" or "FAILED")
print("HTTP code:", code)
print("Response headers:")
if type(headers) == "table" then
  for k, v in pairs(headers) do
    print(k, ":", v)        
  end
end
print( table.concat(resp) )

to get json file set MIME type in request table: content_type = 'application/json'

 body, code, headers= https.request{
    url = link,
    filename = file,
    disposition  = 'attachment',         -- if attach
    content_type = 'application/json',
    headers = { 
                ['Referer'] = link,
                ['Connection'] = 'keep-alive'
                    },         
    sink = ltn12.sink.table(resp)    
 }  
like image 2
Mike V. Avatar answered Nov 17 '22 12:11

Mike V.