4

In python (and my browser), I am able to send a request to https://www.devrant.com/api/devrant/rants?app=3&sort=algo&limit=10&skip=0 and get a response, as expected, but with Lua, I get HTTP/1.1 301 Moved Permanently. Here is what I have tried so far:

http = require("socket.http");
print(http.request("https://www.devrant.com/api/devrant/rants?app=3&sort=algo&limit=10&skip=0")

which outputs an HTTP error page (moved permanently) and

301    table: 0x8f32470    http/1.1 301 Moved Permanently

the table's contents are:

location    https://www.devrant.com/api/devrant/rants?app=3&sort=algo&limit=10&skip=0
content-type    text/html
server    nginx/1.10.0 (Ubuntu)
content-length    194
connection    close
date    Mon, 11 Dec 2017 01:41:35

Why does only Lua get this error? If I request to google, I get the google home page HTML. If I request to status.mojang.com, I get the mojang server statuses in a JSON response string, so the socket is functional for certain.

AlgoRythm
  • 1,196
  • 10
  • 31

1 Answers1

7

It's because you are using socket.http to request a page from https URL; since socket.http doesn't handle https, it sends the request to port 80, which gets forwarded to https URL, but socket library doesn't follow that redirect, as it doesn't "know" what to do with https, so it simply reports 301.

You need to install and use luasec and use ssl.https instead of socket.http, which will make it work.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56