[SOLVED] How to implement an HTTPS request?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
tgftw
Prole
Posts: 2
Joined: Wed May 22, 2024 4:43 pm

[SOLVED] How to implement an HTTPS request?

Post by tgftw »

I feel like this should be a common question but due to the number of times "HTTPS" came up in forums (every time something is linked) it was very hard to find a recent post regarding this, and Google results are from many years ago. Apologies if it feels like I didn't search for my answer before making a post.

All I want is to access a RESTful API - specifically https://pokeapi.co/ - so for example, if you visit https://pokeapi.co/api/v2/pokemon/ditto and see a bunch of raw JSON, that's what I'm trying to access within Love2d and eventually convert it into a Lua table.

So I made my way over to https://love2d.org/wiki/Networking and checked out the available options for networking. "socket" seems to be the most simple and well supported. However, when using "socket," I've come to realize it cannot support HTTPS. I only get 301 errors, and through my research it seems that's a result of an attempted https:// to http:// redirect or vice versa, I may have the details backward.

I've tried changing the URL manually from https:// to http:// with no effect. I've tried appending a slash to the URL which has worked for some people in similar situations across the internet but it also had no effect, simply returning a 301 error through "socket."

It seems to be the lack of HTTPS support with "socket" that is the actual problem, so I've checked out the other networking options. "lua-https" seems to be what I need, and I've spent the past few hours trying to figure out how to use CMAKE to compile it from the source since LOVE 12 isn't released yet... but I could really use some guidance on how to implement "lua-https," I find the whole process very confusing. CMAKE seems to think the project files included within the "lua-https" source are corrupt or somehow incorrect? I'm positive it's just USER ERROR, but I don't really know where to look for the level of support my feeble brain can actually understand.

Is there a reference for a step-by-step installation of a Lua module into Love2d, building from source? Is there something about the HTTPS module that is commonly misunderstood? Am I just too dumb to continue programming? Haha.

I'm happy to provide source, although I don't really know how it would help, here's what I tried with "socket", and I was unable to ever get the "lua-https" module built so I didn't get to make any attempts with that.

Code: Select all

local http = require("socket.http")

function love.load()
    testUrl = "http://pokeapi.co/api/v2/pokemon/?limit=151"
    restTest, restTwo = http.request(testUrl)
end

function love.draw()
    love.graphics.print(restTest .. "\n" .. restTwo)
end
I've tried the following different iterations of testUrl based on advice I found on various forums:

"http://pokeapi.co/api/v2/pokemon?limit=151" (no trailing slash)
"http://pokeapi.co/api/v2/pokemon/?limit=151" (trailing slash)
"https://pokeapi.co/api/v2/pokemon?limit=151" (https)
"https://pokeapi.co/api/v2/pokemon/?limit=151" (https w/ trailing slash)

All of these addresses work when typed into a web browser.

Any advice would be appreciated, I think I know what I need to do to implement "lua-https" in concept, but in practice I feel like I'm hitting a brick wall learning how to compile with CMAKE and all that. I should also mention that the end goal of this project is to be able to play this game on mobile... and I feel like if I'm needing to compile from source and developing on Windows that I'll have to redo it when it comes time to actually release on Android so I should figure out how to actually do it myself rather than just have somebody pass me a precompiled binary. But perhaps I'm really missing the mark altogether.

Hep meh? <3
Last edited by tgftw on Thu May 23, 2024 1:06 pm, edited 1 time in total.
User avatar
togFox
Party member
Posts: 801
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How to implement an HTTPS request?

Post by togFox »

I had the same experience as yourself 6 months ago. Solution here:

https://www.love2d.org/forums/viewtopic ... ps#p257794
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
tgftw
Prole
Posts: 2
Joined: Wed May 22, 2024 4:43 pm

Re: How to implement an HTTPS request?

Post by tgftw »

togFox wrote: Thu May 23, 2024 6:41 am I had the same experience as yourself 6 months ago. Solution here:

https://www.love2d.org/forums/viewtopic ... ps#p257794
You're a legend, this directed me to exactly what I needed to do - something which I'll say has been loosely explained numerous times across various forum posts over the years, but not in a way that an idiot such as myself could understand. To put it simply, the wiki could use a bit of handholding on this topic, I think - I'd imagine a lot of people interested in using Love2d may not be familiar with or feel comfortable with collecting resources from Github and how that procedure might work, it's a bit daunting to the uninitiated.

Thanks again. Solved!
User avatar
togFox
Party member
Posts: 801
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: [SOLVED] How to implement an HTTPS request?

Post by togFox »

The wiki is open for any one to contribute. I've done so myself many times. :)
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests