Page 1 of 1

[SOLVED] Get Output from Webpage

Posted: Wed Oct 28, 2015 1:31 pm
by TheMeq
Hi all, I'm trying to use the tutorial on the wiki for lure to get output from a webpage, but it looks like the wiki page is outdated, and there doesn't appear to be any up to date tutorials or documentation for the later versions of lure (like the one from the github).

I essentially just need to read a string of text from a webpage, something like this:

Code: Select all

response = query("http://www.domain.com","a=1&b=2","GET")
response = "Hello"
Is there a way to do this?

Re: Get Output from Webpage

Posted: Wed Oct 28, 2015 4:30 pm
by bobbyjones
I think you can use luasockets http module but it will return the html and what ever else is in the page.

Re: Get Output from Webpage

Posted: Thu Oct 29, 2015 11:09 am
by TheMeq
Got what I needed with the following, just incase anyone else wants it. Please note, it is blocking, my next step is to figure out how to make it non-blocking, but for the game I'm developing, it's not too much of an issue. :D

Code: Select all

http = require("socket.http")
ltn12 = require("ltn12")

function request(urlget)
    local result_table = {}
    client,r,c,h = http.request{
    url = urlget,
    sink = ltn12.sink.table(result_table)}
    local result = table.concat(result_table)
    return result
end

function love.load()
    b = request("http://www.themeq.xyz/ethereal/api.php")
    a = "Hello"
    if a == b then
        print("Matched!")
    else
        print("Not Matched!")
    end
    print(b)
end
Enjoy :D

Re: [SOLVED] Get Output from Webpage

Posted: Thu Oct 29, 2015 8:52 pm
by bobbyjones
I have a non-blocking variant that uses bartbes' async lib if you want it.

Re: [SOLVED] Get Output from Webpage

Posted: Thu Oct 29, 2015 8:59 pm
by TheMeq
If you could share, that would be awesome!

Re: [SOLVED] Get Output from Webpage

Posted: Thu Oct 29, 2015 10:29 pm
by bobbyjones
These are the two relevant files. My utils.request is like your http wrapper thing. and in Todo.lua all I do is require async and add the requests as jobs. When you require async it will pass around one instance. So you do not need to worry about it being a global.

https://github.com/Bobbyjoness/To-Do-Ap ... /utils.lua
https://github.com/Bobbyjoness/To-Do-Ap ... s/Todo.lua
https://github.com/Bobbyjoness/To-Do-Ap ... r/main.lua