Save cookies between two curl requests Save cookies between two curl requests curl curl

Save cookies between two curl requests


Use the --cookie-jar or --dump-header parameter to save received cookies to a file. The --cookie parameter can read back the cookies from that file later.

-b, --cookie <name=data>

(HTTP) Pass the data to the HTTP server as a cookie. It is supposedly the data previously received from the server in a "Set-Cookie:" line. The data should be in the format "NAME1=VALUE1; NAME2=VALUE2".

If no '=' symbol is used in the line, it is treated as a filename to use to read previously stored cookie lines from, which should be used in this session if they match. Using this method also activates the cookie engine which will make curl record incoming cookies too, which may be handy if you're using this in combination with the -L, --location option. The file format of the file to read cookies from should be plain HTTP headers (Set-Cookie style) or the Netscape/Mozilla cookie file format.

The file specified with -b, --cookie is only used as input. No cookies will be written to the file. To store cookies, use the -c, --cookie-jar option.

Exercise caution if you are using this option and multiple transfers may occur. If you use the NAME1=VALUE1; format, or in a file use the Set-Cookie format and don't specify a domain, then the cookie is sent for any domain (even after redirects are followed) and cannot be modified by a server-set cookie. If the cookie engine is enabled and a server sets a cookie of the same name then both will be sent on a future transfer to that server, likely not what you intended. To address these issues set a domain in Set-Cookie (doing that will include sub-domains) or use the Netscape format.

If this option is used several times, the last one will be used.

-c, --cookie-jar <file name>

(HTTP) Specify to which file you want curl to write all cookies after a completed operation. Curl writes all cookies previously read from a specified file as well as all cookies received from remote server(s). If no cookies are known, no data will be written. The file will be written using the Netscape cookie file format. If you set the file name to a single dash, "-", the cookies will be written to stdout.

This command line option will activate the cookie engine that makes curl record and use cookies. Another way to activate it is to use the -b, --cookie option.

If the cookie jar can't be created or written to, the whole curl operation won't fail or even report an error clearly. Using -v will get a warning displayed, but that is the only visible feedback you get about this possibly lethal situation.

Since 7.43.0 cookies that were imported in the Set-Cookie format without a domain name are not exported by this option.

If this option is used several times, the last specified file name will be used.

-D, --dump-header <file>

Write the protocol headers to the specified file.

This option is handy to use when you want to store the headers that an HTTP site sends to you. Cookies from the headers could then be read in a second curl invocation by using the -b, --cookie option! The -c, --cookie-jar option is a better way to store cookies.

When used in FTP, the FTP server response lines are considered being "headers" and thus are saved there.

If this option is used several times, the last one will be used

Alternatively, instead of using the command-line cURL app, write some code that uses the libCurl library. That will give you more direct control over cookie handling. libCurl has several features related to HTTP cookies:

Options for curl_easy_getinfo():

Options for curl_easy_setopt():

Then you can store the cookies however you want, and assign them as needed to later HTTP sessions.


curl -b cookie.txt -c cookie.txt <url> or curl --cookie cookie.txt --cookie-jar cookie.txt <url> will both store and send saved cookies.


I would like to thank everyone here for helping me find a solution to login to my wordpress site using CURL. This is my little contribution, hope that it will also help some one who stumble across this in the future

For windows 10 build newer than 1706 you can use curl from the command line.

curl -c c:\Users\<your-name>\Desktop\cookie.txt -F "log=<your username>" -F "pwd=<your-pwd>" https://acme.net/wp-login.php

you won't see any response in your terminal. it just refreshes itself but your cookie is already stored.

then type in the below to read and send the cookies you just stored.

curl -b c:\Users\<your-name>\Desktop\cookie.txt -L https://acme.net/my-profile/

the entire webpage that should only be accessible for logged in users will be rendered inside your terminal including any data that is loaded dynamically via javascript.

Even if the end point of your WP login url is '/register/log-in' or '/login' i still recommend you use '/wp-login.php' as the endpoint. That's because some WP themes have a login nonce in a hidden input on their login page. The native WP login page does not have this.

other sources you can refer to:

https://wpmayor.com/login-to-wordpress-dashboard-via-curl/

https://gist.github.com/subfuzion/08c5d85437d5d4f00e58

https://www.youtube.com/watch?v=B4ilccLUQVs

https://makandracards.com/makandra/48262-how-to-use-cookies-with-curl