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

Problems with http.get()

Started by tommykent1210, 30 July 2012 - 09:44 AM
tommykent1210 #1
Posted 30 July 2012 - 11:44 AM
hmmm, after solving my XML problem I hit another snag :ph34r:/>/>


I enabled API_http in the config files for CC, so that isnt the problem

I have:

local UpdateURL = getConfigParam("UpdateURL")
writeBanner(UpdateURL)
sleep(5)
if UpdateURL ~= nil then
local OScode = http.get("http://google.com/")

write(OScode) -- my write to screen function
if OSCode ~= nil then
f = fs.open("osp","w")
local OSLines = textutils.unserialize(OSCode)
for x = 0, #OSLines do
f.writeLine(OSLines[x])
end
f.close()
end
end

And the output is:

table: 62101a6c

Any ideas?

Thanks
Tom
MysticT #2
Posted 30 July 2012 - 02:54 PM
The http functions return a file handle, wich you use to read the content returned like you do with a file.
Example:

local content = http.get("some_url")
print(content.readAll()) -- read and print the content
content.close() -- not sure if it's needed, but just in case
tommykent1210 #3
Posted 30 July 2012 - 05:36 PM
Ahhh :)/>/> that makes sense thanks

Maybe that should be in the wiki? It isn't explained :ph34r:/>/>
KingMachine #4
Posted 30 July 2012 - 06:18 PM
I failed
EDIT: I need to spend a bit more time studying what I'm looking at. I just derped HARD.
Lyqyd #5
Posted 30 July 2012 - 06:40 PM
Ahhh :)/>/> that makes sense thanks

Maybe that should be in the wiki? It isn't explained :ph34r:/>/>

I'll update the wiki tonight if it hasn't already been.

MysticT, I assume from the code you posted that the file handle returned is equivalent to what you'd get from fs.open() rather than io.open(). Is that correct, or should I test tonight to confirm?
MysticT #6
Posted 30 July 2012 - 07:27 PM
MysticT, I assume from the code you posted that the file handle returned is equivalent to what you'd get from fs.open() rather than io.open(). Is that correct, or should I test tonight to confirm?
Yes, that's right. The handle returned (that is actually a table) has the same functions as one obtained from opening a file in read mode (fs.open("path", "r")) wich are:

handle.readLine()
handle.readAll()
handle.close()

Also, the io api is defined in lua (inside rom/apis), and io.open it just wraps a file handle returned by fs.open.
Lyqyd #7
Posted 31 July 2012 - 01:04 AM
MysticT, I assume from the code you posted that the file handle returned is equivalent to what you'd get from fs.open() rather than io.open(). Is that correct, or should I test tonight to confirm?
Yes, that's right. The handle returned (that is actually a table) has the same functions as one obtained from opening a file in read mode (fs.open("path", "r")) wich are:

handle.readLine()
handle.readAll()
handle.close()

Also, the io api is defined in lua (inside rom/apis), and io.open it just wraps a file handle returned by fs.open.

I'm quite aware of the distinction between the two, and the nature of each. ;-) Thanks for the clarification, though!