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

the HTTP API and the cookies

Started by Ta©ti_Tac0Z, 12 July 2018 - 08:32 PM
Ta©ti_Tac0Z #1
Posted 12 July 2018 - 10:32 PM
am makeing a webbrowser in computercraft (yes a webbrowser… not useing html - by good resons, this is not about the webbrowser tho… all ready have 1000 lines of code + apis…)
so simple qestion how does cookies work in computercrafts http api?


– to all thoes who wants to say: "a webrowser :P/> wow come on dude" then stop and watch out for your self…
(if you strucle to understand my point: i don't want any: "negative" talk about my project thank you)
KingofGamesYami #2
Posted 12 July 2018 - 10:49 PM
Cookies are in the headers, of course. You can't read the headers in any version of computercraft that has been officially released, but the latest version includes the ability to do so through the .getResponseHeaders method.

Reference: https://github.com/dan200/ComputerCraft/blob/master/src/main/java/dan200/computercraft/core/apis/http/HTTPRequest.java
Ta©ti_Tac0Z #3
Posted 12 July 2018 - 10:51 PM
yes that brigs me to my next qestion how to get headers send by a php code and so on?

thanks for your fast response
SquidDev #4
Posted 12 July 2018 - 10:54 PM
I first recommend reading this MDN article on cookies, as it explains how they are transmitted. Basically, we need to things:
  • A way of sending cookies to the server
  • A way of reading cookies the server sets
The first is easy - the http.get methods allows setting a header, so we can simply pass that in:

local response = http.get("https://httpbin.com/cookies", { Cookie = "my_cookie=tasty" })
print(response.readAll()) --# { "cookies": { "my_cookie": "tasty" } }

Getting them can be done with recent version of CC's .getResponseHeaders. You'll need to find the Set-Cookie header and consume it:

local response = http.get("https://httpbin.com/cookies/set/my_cookie/tasty")
print(response.getResponseHeader()["Set-Cookie"]) --# my_cookie=tasty

Yes, I realise Yami has said most of this already, but I was half way through my comment by that point :P/>.
Edited on 12 July 2018 - 08:55 PM
Ta©ti_Tac0Z #5
Posted 12 July 2018 - 10:59 PM
ahhh getResponseHeader() that function isn't in the documentation wired

- thanks
Ta©ti_Tac0Z #6
Posted 12 July 2018 - 11:05 PM
but the latest version includes the ability to do so through the .getResponseHeaders method.
wait what version was this added?
KingofGamesYami #7
Posted 13 July 2018 - 02:32 AM
It was added in 1.80pr0 or 1.80pr1 (unreleased).

The builds available on https://cc.crzd.me should include it.
Ta©ti_Tac0Z #8
Posted 13 July 2018 - 10:23 AM
i have the version for 1.10 (downloaded from there) and i get a "tryed to call nil" error ):


h,r = http.get("http://cc.noahtg.com/headertest.php")
if h == nil then error(r) end
print(h.readAll())
print(h.getResponseCode())
print(textutils.serialise(h.getResponseHeader())) //did i spell it wrong? maybe?
h.close()

EDIT: name of jar file: ComputerCraft-1.80pr0-build10 i'll try downloading it again maybe i get the right one
Edited on 13 July 2018 - 08:25 AM
Ta©ti_Tac0Z #9
Posted 13 July 2018 - 10:32 AM
didn't have the exact version from there. i downloaded: ComputerCraft-SquidDev-CC-ComputerCraft-feature-minecraft-1.9.4-1.80pr1-build2 but
still reports an tryed to call nil error

so did i spell getResponseHeader worng or some thing?

does it only work for versions for the newer versions of minecraft?
Bomb Bloke #10
Posted 13 July 2018 - 10:54 AM
does it only work for versions for the newer versions of minecraft?

Unfortunately, newer versions of CC are not available for older versions of Minecraft. The one you've got there is roughly half a year old.
SquidDev #11
Posted 13 July 2018 - 12:53 PM
Unfortunately, newer versions of CC are not available for older versions of Minecraft. The one you've got there is roughly half a year old.
It's built from the 1.80pr1 code so should have it. I'll have to double check this though.
Bomb Bloke #12
Posted 13 July 2018 - 04:03 PM
In the meantime, noah, you may as well check what is available through your web handle:

local h,r = http.get("http://cc.noahtg.com/headertest.php")
if h == nil then error(r) end
for key, val in pairs(h) do print(key .. " is a " .. type(val)) end
h.close()
Ta©ti_Tac0Z #13
Posted 13 July 2018 - 08:19 PM
ok then, i'll do
Ta©ti_Tac0Z #14
Posted 13 July 2018 - 08:26 PM
ooooo ha ha emmmmm getResponseHeaders not getResponseHeader make sense… am sorry :P/> (am sorry for wasteing your boths time)
Ta©ti_Tac0Z #15
Posted 13 July 2018 - 09:43 PM
hmmmm so i know your guys aren't a liveing search engine but to define one single cookie we do like so

http.get("http://some.website.com", {cookie = "foo=bar"})
but what if i want to have multiple cookies?

{cookie = "foo=bar&amoutofhornsonaunicorn=1"} --doesn't work
{cookie = "foo=bar", cookie = "amoutofhornsonaunicorn=1"} --nether does this witch makes sense (the 2. will overwrite the 1.)
anyone of you know how this is done?

found out it sems that

{cookie = "foo=bar;amoutofhornsonaunicorn=1"}

works
Edited on 13 July 2018 - 07:39 PM