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

Simple Stupid Question about Http request

Started by se7en, 09 July 2014 - 07:16 AM
se7en #1
Posted 09 July 2014 - 09:16 AM
Hi there again!

And here i am, again with a crazy problem or maybe its a glitch?

Ok so what im on:
Spoiler

local news_handle = http.get("http://94.23.248.33/skyblock/news.txt")
if not news_handle then
news = "Cant load any news from server"
else
local news = news_handle.readAll()
news_handle.close()
end
print(news)


Working 100%.

But when i do something like:
Spoiler



m = peripheral.wrap("back")
m.clear()
term.redirect(m)
m.setTextScale(1)
m.setCursorPos(1,1)
m.setTextColour(512)

local news_handle = http.get("http://94.23.248.33/skyblock/news.txt")
if not news_handle then
  news = "Cant load any news from server"
else
  local news = news_handle.readAll()
  news_handle.close()
end

m.setTextColour(colors.orange)
m.setCursorPos(16,7)
m.write("Welcome on our server!")
m.setTextColour(1)
m.setCursorPos(15,10)
m.write("NEWS: ")
m.setTextColour(32)
m.write(news)


All its showing is 'Welcome on our server!', and line below 'NEWS: '
No news are loading, why?

edit: when i changed to "m.write(tostring(news))" it shows "nil" ?!
Edited on 09 July 2014 - 07:18 AM
theoriginalbit #2
Posted 09 July 2014 - 09:33 AM
the problem is with your use of the local keyword here

local news = news_handle.readAll()
this means once the else statement is finished the news variable no longer exists, hence it being nil. remove the word local from the start of that line and it will fix the problem. I actually suggest moving it up to above the if.

local news
if not news_handle then
  news = "can't load"
else
  news = news_handle.readAll()
end
se7en #3
Posted 09 July 2014 - 09:39 AM
the problem is with your use of the local keyword here

local news = news_handle.readAll()
this means once the else statement is finished the news variable no longer exists, hence it being nil. remove the word local from the start of that line and it will fix the problem. I actually suggest moving it up to above the if.

local news
if not news_handle then
  news = "can't load"
else
  news = news_handle.readAll()
end

omg, thanks
now i see i need to learn more xD
ure all amazing, ty again !