This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Jiihon's profile picture

http.get() returns a table, how to get the content?

Started by Jiihon, 21 July 2012 - 07:26 PM
Jiihon #1
Posted 21 July 2012 - 09:26 PM
Hello

I tried this code, directly copied from the wiki:
local sExample = http.get("http://mywebpage.com/")
write(sExample)
and it outputs
bios:48: bad argument: string expected, got table

If we use print() instead of write() it outputs
table: 2e619eb7
instead (where 2e619eb7 is an unique string that changes every time I run the script).

So what should I do to get the contents of the webpage? What I want to accomplish is something similar to
if sExample == "1" then
redstone.setoutput(top,true)
etc…
But this is not possible since I can't find any way to access the content inside the table. Any suggestions?


Solution:

sExample = http.get("http://example.com/")
print(sExample.readAll())
Edited on 21 July 2012 - 09:12 PM
Lyqyd #2
Posted 21 July 2012 - 09:35 PM
Hello

I tried this code, directly copied from the wiki:
local sExample = http.get("http://mywebpage.com/")
write(sExample)
and it outputs
bios:48: bad argument: string expected, got table

If we use print() instead of write() it outputs
table: 2e619eb7
instead (where 2e619eb7 is an unique string that changes every time I run the script).

So what should I do to get the contents of the webpage? What I want to accomplish is something similar to
if sExample == "1" then
redstone.setoutput(top,true)
etc…
But this is not possible since I can't find any way to access the content inside the table. Any suggestions?

When working with tables, the ipairs and pairs functions are quite handy. From the lua documentation:

ipairs():
Returns three values: an iterator function, the table t, and 0, so that the construction

for i,v in ipairs(t) do [i]body[/i] end
will iterate over the pairs (1,t[1]), (2,t[2]), ···, up to the first integer key absent from the table.

pairs():
Returns three values: the next function, the table t, and nil, so that the construction

for k,v in pairs(t) do body end
will iterate over all key–value pairs of table t.
See function next for the caveats of modifying the table during its traversal.

So, you could use this to print the source code of the web page:


for k,v in ipairs(sExample) do
	print(v)
end
Jiihon #3
Posted 21 July 2012 - 09:51 PM
So, you could use this to print the source code of the web page:
for k,v in ipairs(sExample) do
	print(v)
end

I'm sorry to tell you that that piece of code returned nothing :)/>/> No console output.
However, if I make a table myself it prints it without problems:
sExample = {1,2,"hello","world"}
returns
1
2
hello
world
as expected

I can confirm that the webpage was indeed requested from the server by looking in the server log.


EDIT
I tried pairs() instead of ipairs()

for k,v in pairs(sExample) do
  print(k,v)
end
output:

ReadLine  function: 289b4ec2
ReadAll   function: 6c4fb761
close      function: 659078d4

At least some progress when the console is actually outputting something. But I don't understand what to do with these functions, neither what they do there.
Lyqyd #4
Posted 21 July 2012 - 10:04 PM
Edit to address your edit: It's basically returning a file handle, as if you had opened the webpage's contents from a local file with fs.open(). Use (from your code above, for example):


sExample.readLine()

You'll probably want to use a loop with that to move everything over to a table of just the contents of the page, and then use that table to do whatever else.
Jiihon #5
Posted 21 July 2012 - 10:24 PM
That's… odd. Try using pairs() instead of ipairs(). If it's returning a table, it should be able to print the values.
see edit.
I should probably find out some way to execute the ReadAll function…
Lyqyd #6
Posted 21 July 2012 - 10:46 PM
That's… odd. Try using pairs() instead of ipairs(). If it's returning a table, it should be able to print the values.
see edit.
I should probably find out some way to execute the ReadAll function…

Edited to address the edit.
Jiihon #7
Posted 21 July 2012 - 11:00 PM
Edit to address your edit: It's basically returning a file handle, as if you had opened the webpage's contents from a local file with fs.open(). Use (from your code above, for example):

sExample.readLine()
You'll probably want to use a loop with that to move everything over to a table of just the contents of the page, and then use that table to do whatever else.

I got output now. Thanks for your help!
Code used if anyone else is trying to accomplish this:

sExample = http.get("http://example.com/temp/test.php")
for k,v in pairs(sExample) do
  print(sExample.readAll())
end

ReadLine will probably also be useful in some way, maybe in order to separate multiple argument. For example the first line tells if the lights should turn on, the second line tells if the door should be locked and so on.
Lyqyd #8
Posted 21 July 2012 - 11:01 PM
Edit to address your edit: It's basically returning a file handle, as if you had opened the webpage's contents from a local file with fs.open(). Use (from your code above, for example):

sExample.readLine()
You'll probably want to use a loop with that to move everything over to a table of just the contents of the page, and then use that table to do whatever else.

I got output now. Thanks for your help!
Code used if anyone else is trying to accomplish this:

sExample = http.get("http://example.com/temp/test.php")
for k,v in pairs(sExample) do
  print(sExample.readAll())
end

ReadLine will probably also be useful in some way, maybe in order to separate multiple argument. For example the first line tells if the lights should turn on, the second line tells if the door should be locked and so on.

You don't need the loop at that point, just:


page = http.get("http://example.com/temp/test.php")
print(page.readAll())