How are cookies passed in the HTTP protocol?

HttpCookies

Http Problem Overview


How are cookies passed in the HTTP protocol?

Http Solutions


Solution 1 - Http

The server sends the following in its response header to set a cookie field.

Set-Cookie: name=value

If there is a cookie set, then the browser sends the following in its request header.

Cookie: name=value

See the HTTP Cookie article at Wikipedia for more information.

Solution 2 - Http

Cookies are passed as HTTP headers, both in the request (client -> server), and in the response (server -> client).

Solution 3 - Http

Apart from what it's written in other answers, other details related to path of cookie, maximum age of cookie, whether it's secured or not also passed in Set-Cookie response header. For instance:

Set-Cookie: name=value[; expires=date][; domain=domain][; path=path][; secure]


However, not all of these details are passed back to the server by the client when making next HTTP request.

You can also set HttpOnly flag at the end of your cookie, to indicate that your cookie is httponly and must not allowed to be accessed, in scripts by javascript code. This helps to prevent attacks such as session-hijacking.

For more information, see RFC 2109. Also have a look at Nicholas C. Zakas's article, HTTP cookies explained.

Solution 4 - Http

create example script as resp :

#!/bin/bash

http_code=200
mime=text/html

echo -e "HTTP/1.1 $http_code OK\r"
echo "Content-type: $mime"
echo "Set-Cookie: name=F"
echo

then make executable and execute like this.

./resp | nc -l 12346

open browser and browse URL: http://localhost:12346 you will see Cookie value which is sent by Browser

[aaa@bbbbbbbb ]$ ./resp | nc -l -p 12346
GET / HTTP/1.1
Host: xxx.xxx.xxx.xxx:12346
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,ru;q=0.6
Cookie: name=F

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjaiView Question on Stackoverflow
Solution 1 - HttpdeinstView Answer on Stackoverflow
Solution 2 - HttpDouglas LeederView Answer on Stackoverflow
Solution 3 - HttpMangu Singh RajpurohitView Answer on Stackoverflow
Solution 4 - HttpFariZView Answer on Stackoverflow