0

I'd like to understand how to save cookies that the application writes to document.cookie (https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie), as I've noticed that the common methods wget/curl to save to a file does not work for this particular case, it seems.

Assuming the web app writes cookies:

document.cookie = name + "=" + value  + expires + "; path=/";

For example,

wget --keep-session-cookies --save-cookies cookies.txt "http://foobar/app-generates-cookie.html"

or the curl,

curl --cookie-jar cookie.txt http://foobar/app-generates-cookie.html

Both wget/curl does not work for the app case above.

1 Answer 1

0

It doesn't work if you save your cookies on the client-side with JavaScript in your HTML code. Wget and curl don't interpret any JavaScript code, they just return the response sent by the server.

You need to set you cookies on the server-side, for example with setcookie in PHP. This way cookies are sent in the HTTP response header Set-Cookie field and can be saved by wget/curl.

A response header (output from wget using the -S / --server-response option) with a cookie looks like this:

HTTP/1.1 200 OK
Date: Fri, 01 Nov 2019 00:33:05 GMT
Server: Apache/2.4.25 (Debian)
Set-Cookie: testcookie=myvalue; expires=Fri, 01-Nov-2019 01:33:05 GMT; Max-Age=3600
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

The cookie was set in a PHP page using:

<?php setcookie("testcookie", "myvalue", time()+3600); ?>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .