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

http.get returns nil

Started by houseofkraft, 13 September 2016 - 10:13 AM
houseofkraft #1
Posted 13 September 2016 - 12:13 PM
Hi Guys!

I am currently working on the auto updater for CarbonOS again. It is so good! No more problems except one. http.get returns nil for some reason. I think the URL is correct. I even copied and pasted the Raw URL and it still returns nil.

Code:
local nextFile = "/.os"
local files = {
    [1] = {
	    "/System/Images/boot",
	    "https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/System/Images/boot"
    },
    [2] = {
	    "startup",
	    "https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/startup"
    },
    [3] = {
	    "/Programs/LuaIDE/program",
	    "https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/Programs/LuaIDE/program"
   },
   [4] = {
	   "/Programs/Sketch/program",
	   "https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/Programs/Sketch/program"
   },
   [5] = {
	   "/Desktop/LuaIDE",
	   "https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/Desktop/LuaIDE"
  },
  [6] = {
	  "/Desktop/Sketch",
	  "https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/Desktop/Sketch"
  },
  [7] = {
	  "/System/Images/desktop",
	  "https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/System/Images/desktop"
},
[8] = {
	 "/System/settings",
	 "https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/System/settings"
},
[9] = {
	 "/System/autoupdater",
	 "https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/System/autoupdater"
},
[10] = {
	  "/System/.version",
	  "https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/System/.version"
},
[11] = {
	  "/os",
	  "https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/os"
}
}
remoteVersion = http.get("https://raw.githubusercontent.com/Carbon-OS/CarbonOS/master/System/.version")
local localVersion = fs.open("System/.version", "r")
local currentVer = localVersion.readAll()
local rVersion = remoteVerion.readAll()
local lVersion = localVersion.readAll()
localVersion.close()
if rVersion ~= lVersion then
    print("Downloading Update...")
    print("Your Verison: ", lVersion)
    print("New Version: ", rVersion)
    for k, v in pairs(files) do
	    local currentFile = fs.open(v[1], "w")
	    local remoteFile = http.get(v[2])
	    if remoteFile ~= nil then
		    currentFile.write(remoteFile.readAll)
	    end
	    currentFile.close()
	    remoteFile.close()
    end
end
local next = fs.open(nextFile, "r")
pcall(loadstring(next.readAll()))
--Put code to run if the os crashes

If you would like to fix it, you can tell me or create a pull request (the easiest way is to create a pull request)
Github: https://github.com/Carbon-OS/CarbonOS/blob/master/System/autoupdater#L49

The highlighted line is the http.get that returns nil

Thank You!
KingofGamesYami #2
Posted 13 September 2016 - 12:24 PM
Are you using an emulator? That may be the problem, as I cannot replicate this.

houseofkraft #3
Posted 13 September 2016 - 12:33 PM
Im not using an emulator. Btw the content of the .version file says 1 if that helps

EDIT: I did an http.checkURL and it said that the url is correct
Edited on 13 September 2016 - 10:37 AM
KingofGamesYami #4
Posted 13 September 2016 - 12:58 PM
What version of computercraft are you using? Does your config allow http? As you can see in my screenshot, http.get works perfectly fine on that url for me.
houseofkraft #5
Posted 13 September 2016 - 01:00 PM
Im using CC 1.74 with the HTTP API enabled
KingofGamesYami #6
Posted 13 September 2016 - 01:15 PM
Are you absolutely certain it is http.get that is returning nil? Remember, computercraft errors sometimes are off by one line number.
houseofkraft #7
Posted 13 September 2016 - 01:21 PM
I'm not too sure because someone told me on the server yesterday that the http.get is returning nil. Because I get an attempt to call nil error when it displays the current version and the new version
KingofGamesYami #8
Posted 13 September 2016 - 02:04 PM
Perhaps it has something to do with the fact that you are using .readAll on the same file handle twice? I'm unsure as to what the effects of that would be.
apemanzilla #9
Posted 13 September 2016 - 02:41 PM
Perhaps it has something to do with the fact that you are using .readAll on the same file handle twice? I'm unsure as to what the effects of that would be.

That would probably be why, after the handle is read it can't be read again IIRC.