• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Downloading file using http.request doesn't follow redirects (1 Viewer)

scls

Newcomer
Joined
Mar 10, 2021
Messages
10
Reaction score
2
Hello,

I'm trying to download a file using http.request using Lua 5.4.2 / LuaRocks 3.7.0 / luasocket-3.0rc1-2.src.rock and this code

Lua:
local http = require("socket.http")
url = "https://www.sia.aviation-civile.gouv.fr/dvd/eAIP_07_OCT_2021/Atlas-VAC/PDF_AIPparSSection/VAC/AD/AD-2.LFBI.pdf"
local body, code, headers, status = http.request(url)
if not body then error(ret_code) end
oaci_code = "LFBI"
local fname = string.format("AD-2.%s.pdf", oaci_code)
local f = assert(io.open(fname, 'wb')) -- open in "binary" mode
f:write(body)
f:close()

unfortunately the content of "AD-2.LFBI.pdf" file is not a real PDF file but an HTML file showing a redirect. See below

HTML:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://www.sia.aviation-civile.gouv.fr/dvd/eAIP_07_OCT_2021/Atlas-VAC/PDF_AIPparSSection/VAC/AD/AD-2.LFBI.pdf">here</a>.</p>
</body></html>

So it seems that http.request doesn't follow redirects.

How can I handle that?

Kind regards
 
Last edited:

scls

Newcomer
Joined
Mar 10, 2021
Messages
10
Reaction score
2
After Googling more, I noticed I shouldn't use `http.request` but `ssl.https` because of the use of secured protocol

Unfortunately that's not so easy...

Bash:
$ sudo luarocks install luasec
Warning: falling back to wget - install luasec to get native HTTPS support
Installing https://luarocks.org/luasec-1.0.2-1.src.rock


Error: Could not find header file for OPENSSL
  No file openssl/ssl.h in /usr/local/include
  No file openssl/ssl.h in /usr/include
  No file openssl/ssl.h in /include
You may have to install OPENSSL in your system and/or pass OPENSSL_DIR or OPENSSL_INCDIR to the luarocks command.
Example: luarocks install luasec OPENSSL_DIR=/usr/local

which can simply be fixed by installing libssl-dev

(thanks to Error: Failed installing dependency: https://luarocks.org/luaossl-20180708-0.src.rock - Could not find header file for CRYPTO · Issue #632 · leafo/lapis )

Bash:
apt-get install libssl-dev


and use instead

Lua:
local https = require("ssl.https")
local body, code, headers, status = http.request(url)

Here is code
 
Last edited:
Top