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

http.request problems

Started by DannySMc, 20 May 2015 - 03:01 PM
DannySMc #1
Posted 20 May 2015 - 05:01 PM
I do not understand why this does not output anything? it just comes back as nil? but when I use http.post() it comes back?

I have tested this via the post and it DOES work, try it and do readAll() and it will show you.. but for some reason http.request just comes back with nil? everytime?

Someone help :/

CODE:
Spoiler
function getapi()
	repeat
		local stat1, err1 = pcall(function()
			aa = aa or {}
			a = http.get("https://projects.dannysmc.com/apis/progutils.lua")
			a = a.readAll()
			local env = {}
			a = loadstring(a)
			local env = getfenv()
			setfenv(a, env)
			local status, err = pcall(a, unpack(aa))
			if (not status) and err then
				printError("Error loaing api")
				return false
			end
			local returned = err
			env = env
			_G["progutils"] = env
		end)
	until stat1 == true
end

function drawchatfile(tchat)
	print(type(tchat))
	if type(tchat) == "table" then
		tchat = textutils.unserialize(tchat.readAll())
		if tchat <= i then
			for i=1, 16 do
				draw.texta("U: "..tchat[i][2].." | M: "..tchat[i][4], 1, i+1, false, "white", "black")
			end
		end
	end
end

function test()
	req = http.request(url, "ccsys=babble&amp;cccmd=get_chat&amp;room=test")
	count = 0
	while true do
		local args = { os.pullEvent() }
		if args[1] == "http_success" then
			count = count + 1
			draw.texta("Received: "..count, 1, 1, false, "grey", "white")
			drawchatfile(req)
			req = http.request(url, "ccsys=babble&amp;cccmd=get_chat&amp;room=test")
		elseif args[1] == "http_failure" then
			req = http.request(url, "ccsys=babble&amp;cccmd=get_chat&amp;room=test")
		end
	end
end
getapi()
test()
Creator #2
Posted 20 May 2015 - 05:03 PM
I think that it will queue an event, making asynchronous

Actually I am pretty sure
DannySMc #3
Posted 20 May 2015 - 05:29 PM
I think that it will queue an event, making asynchronous

Actually I am pretty sure

So how do I get it back? Like how do I actually make it async? Any possible amends of the code for me?:D/>
Lignum #4
Posted 20 May 2015 - 05:37 PM
Take a look at this wiki page, it shows that http.request doesn't actually give anything back, which is why it's nil. It queues a http_success event with the http handle as the second parameter. If the request failed, http_failure is queued.