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

HTTP requests get mixed up

Started by _removed, 01 June 2015 - 03:02 PM
_removed #1
Posted 01 June 2015 - 05:02 PM
For a system I'm using, the client constantly checks the server to see if it responds. Above that, I have other HTTP requests. They are sending the wrong details and as a result, returning the wrong values. How could I avoid this?
KingofGamesYami #2
Posted 01 June 2015 - 05:04 PM
Define 'wrong details' and 'wrong values'. Also, how should I know what you're doing if you post no code?
_removed #3
Posted 01 June 2015 - 05:11 PM
Its using an API thats not yet documented in full, the Flare API created by awsumben13. I could show you the code, but you might need some time to process it all, since it is using multiple files.

Basically, from looking at the Flare repo code for core/process/Thread.lua, its just a coroutine that runs forever.



function getCon()
  if http.get("http://www.helixservices.16mb.com/commands.php").readAll() == "You need to enter a command!" then
	return true
  else
	return false
  end
end

function getMoney(username)
  if http.post("http://www.helixservices.16mb.com/commands.php", "command=getMoney&username=" .. textuils.urlEncode(username)).readAll() ~= "false" then
	http.post("http://www.helixservices.16mb.com/commands.php", "command=getMoney&username=" .. textuils.urlEncode(username)).readAll()
  else
	return false
  end
end

-- Basically a coroutine that runs forever
FDK.process:newThread(function()
  if getCon() == false then
	-- Do code here
  end
end)

getMoney("smigger22")

Edited on 01 June 2015 - 03:11 PM
Creator #4
Posted 01 June 2015 - 05:23 PM
You actually want to change ~= "false" to ~= false since it is a boolean and not a string.
KingofGamesYami #5
Posted 01 June 2015 - 05:34 PM
You actually want to change ~= "false" to ~= false since it is a boolean and not a string.

Stuff returned by http.post / http.get is always a string, not a boolean.
_removed #6
Posted 01 June 2015 - 05:46 PM
You actually want to change ~= "false" to ~= false since it is a boolean and not a string.

Stuff returned by http.post / http.get is always a string, not a boolean.

I know that, but whenever I run getMoney, it returns You need to enter a command!
flaghacker #7
Posted 01 June 2015 - 06:00 PM
Is this your full code? If so, how does the program 'say' something? I dont see any print statements in there.

If you're really sure your requests are mixed up, then that's because http.post uses an event interally, and those can get mixed up in a multi-coroutine environment. You could try looking at the http source code (by opening it in-game) to figure out what's going on.

A potential solution is to dedicate a single coroutine to http stuff, and run everything there. If you're making an os, consider overwriting the http api to do this automatically.
Edited on 01 June 2015 - 04:00 PM
_removed #8
Posted 01 June 2015 - 07:16 PM
Is this your full code? If so, how does the program 'say' something? I dont see any print statements in there.

If you're really sure your requests are mixed up, then that's because http.post uses an event interally, and those can get mixed up in a multi-coroutine environment. You could try looking at the http source code (by opening it in-game) to figure out what's going on.

A potential solution is to dedicate a single coroutine to http stuff, and run everything there. If you're making an os, consider overwriting the http api to do this automatically.

Its not my full code, I use the Flare debug api that logs to a file.

EDIT: Where is the HTTP source?
Edited on 01 June 2015 - 05:21 PM
flaghacker #9
Posted 01 June 2015 - 08:56 PM

Its not my full code, I use the Flare debug api that logs to a file.

EDIT: Where is the HTTP source?

I can't actually find it, seems like it's not implemented Lua-side… Weird.

Anyway, take a look at this wiki page, It explains the event stuff a little bit.
MKlegoman357 #10
Posted 01 June 2015 - 08:58 PM
That's a problem with the way CC handles HTTP requests. If you look at the http_success event in the wiki you'll notice that the only way to identify if the event belongs to one http.request or another is the URL you made your request to. So when dealing with multiple http post requests it becomes impossible to detect which one it is.

Luckly to you I have encountered this issue myself and have a very simple solution: just add a random parameter inside the URL. This shouldn't intrfere with the server most of the time. What I like to do is generate two random strings: one for the key and another for the value. An example request may be something like this:


http.post("http://example.com/?hskeoshvdjdjbd=hdjskjsbsbsjj", "key=value")