Home   Archive   Permalink

how to retrieve/send cookies?

I'm writing an application which communicates with a server's custom API, I've got the first part ok which takes this form:
response: read/custom http://some-site/cgi-bin/API [POST "a=1&b=2&c=3"]
This opens a communication with the server, but I need to add code which will accept a cookie and send it on subsequent requests. Any help appreciated, thanks in advance!

posted by:   Jules       26-Oct-2010/2:37:54-7:00

Try these:
or simply this: http://www.rebol.org/search.r?find=cookie&form=yes

posted by:   Endo       26-Oct-2010/3:07:18-7:00

The examples Endo are for code on the server. I think you want code in the client.
If so, this may help:
Cookies is in port/locals/header/set-cookies, eg:
a-connection: open http://www.google.com
g-cookie: a-connection/locals/headers/set-cookie
You can then send the cookie using READ/CUSTOM, something like this (not tested):
    print read/custom http://www.google.com compose/deep [get "q=rebol" header [cookie: (mold g-cookie)]]

posted by:   Sunanda       26-Oct-2010/5:35:39-7:00

Thanks for the feedback guys. It might help if I explain how the API works. The server returns 2 lines in the format:
<status code>
The initial request must have 3 values:
"n" = Number
"b" = batchID
"t" = tokenID
Once you get your sessionID, then there's no need to pass the batchID (b) and tokenID (t) parameters, since the client is going to be identified by the matching cookie and IP address as per the server-side stored session.
So a session might go like this:
Initial request:
2nd request:
3rd request:
At the moment, the batchID and TokenID can be any string, but n (the number) must be an integer in the range 0-36
So I need to send 3 values in the first request, get the cookie, and on subsequent requests send a number + cookie and store each line of the response (<status code>, <numbers>) in separate variables. I've tried writing bits of code to do this but I'm pretty clueless about networking and also a Rebol newbie.

posted by:   Jules       26-Oct-2010/11:08:25-7:00

You might to check the mailing list archive on how to do these things ... or check out my script http-tools.r on rebol.org

posted by:   Graham       2-Nov-2010/23:10:36-7:00

Thanks Graham. I did look at your http-tools.r earlier but have only noticed this in the discussion:
"The functionality provided by http-tools.r is now built in to REBOL.
(That is according to discussion on mail list recently, 21-Nov-2005).
Seek documentation on the /CUSTOM refinement of READ.
Some examples from Gabriele Santilli: {
         read/custom url [post "postdata" [Cookie: "name=value"]]
     The header part is optional, that is you can just:
         read/custom url [post "postdata"]
     "postdata" is whatever your CGI app is expecting, which usually is
     something like "name=value&name2=value2".

posted by:   Jules       9-Nov-2010/5:48:56-8:00

Not quite .. the http protocol does not capture the cookies for you which my script did.
This is better though http://box.lebeda.ws/~hmm/rebol/cookies-daemon_latest.r

posted by:   Graham       11-Nov-2010/22:46:28-8:00