Download a URL with cookies set on the command line with curl

curl -b "session=$session" 'https://adventofcode.com/2023/day/18/input'

December 18, 2023bashoneliners


With the -b flag of curl you can specify cookie values to use in the request header as key-value pairs separated with semicolons.

For example to specify k1=v1 and k2=v2:

curl -b "k1=v1;k2=v2" https://example.com

This is useful for example when you want to download a page using curl but the page requires a logged in user, as the example one-liner. In such situations you can inspect the cookies in a browser to find the cookie that might be used for tracking the user session. In the example of adventofcode.com the cookie is clearly the one named "session". On other sites it may be something else, but the term "session" is widely used and likely to be at least part of the name of the relevant cookie.