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

HTTP API Help

Started by AliasXNeo, 05 January 2013 - 06:32 AM
AliasXNeo #1
Posted 05 January 2013 - 07:32 AM
Greetings,

Here's the bit of code I'm using:



monitor = peripheral.wrap("right")

while true do
  for i = 1, 3 do
    local url = "http://www.aliasprojects.net/mc/message" .. i .. ".txt"

    while true do
      http.request(url)
      event, url, file = os.pullEvent()

      if event == "http_success" then
        monitor.clear()
        monitor.setCursorPos(1, 1)
        txt = file.readAll()
        print(txt)
        monitor.write(txt)
        sleep(10)
        break
      end
    end
  end
end    

Here's the contents of "message1.txt":

Welcome to our server!

Please read the following messages:

Now, when running this program I see the above message appear perfectly on the computer screen, but the monitor screen shows it like this:

Welcome to our server!??Please read the following:

It seems for some reason new lines are being replaced with question marks. I'm not sure if this is because of the file, the HTTP API, or me just using this all wrong. Can anyone point me in the right direction? Cheers.
remiX #2
Posted 05 January 2013 - 08:04 AM
monitor.write will not insert new lines automatically. Save the contents into a table then print it from there.

Try this?

monitor = peripheral.wrap("right")
t = {}
while true do
  for i = 1, 3 do
    local url = "http://www.aliasprojects.net/mc/message" .. i .. ".txt"

    while true do
	  http.request(url)
	  event, url, file = os.pullEvent()

	  if event == "http_success" then
	    monitor.clear()
	    monitor.setCursorPos(1, 1)
	    line = file.readLine()
        repeat table.insert(t, line) line = file.readLine() until not line
	    sleep(10)
	    break
	  end
    end
  end
end

for i = 1, #t do
    print(t[i])
    monitor.setCursorPos(1, i)
    monitor.write(t[i])
end
AliasXNeo #3
Posted 05 January 2013 - 08:45 AM
I don't think you quite got the hang of the what the code is supposed to be doing. However, I took your concept and made it work properly:

function writeLine(monitor, line)
  monitor.write(line)
  x, y = monitor.getCursorPos()
  monitor.setCursorPos(1, y + 1)
end

monitor = peripheral.wrap("right")
while true do
  for i = 1, 3 do
	local url = "http://www.aliasprojects.net/mc/message" .. i .. ".txt"

	while true do
	  http.request(url)
	  event, url, file = os.pullEvent()

	  if event == "http_success" then
		monitor.clear()
		monitor.setCursorPos(1, 1)
		line = file.readLine()
		repeat writeLine(monitor, line) line = file.readLine() until not line
		sleep(10)
		break
	  end
	end
  end
end 

Cheers.
Lyqyd #4
Posted 05 January 2013 - 08:58 AM
Alternatively, you could have redirected output to the monitor with term.redirect(), then used print() again, since print does handle newline characters correctly.
AliasXNeo #5
Posted 05 January 2013 - 09:09 AM
Alternatively, you could have redirected output to the monitor with term.redirect(), then used print() again, since print does handle newline characters correctly.

That saves a lot of code :P/>

monitor = peripheral.wrap("right")
term.redirect(monitor)

while true do
  for i = 1, 3 do
    local url = "http://www.aliasprojects.net/mc/message" .. i .. ".txt"

    while true do
      http.request(url)
      event, url, file = os.pullEvent()

      if event == "http_success" then
        monitor.clear()
        monitor.setCursorPos(1, 1)
        print(file.readAll())
        sleep(10)
        break
      end
    end
  end
end

Thanks mate.