Saturday, October 8, 2022

[SOLVED] How to send same cookies to every curl command?

Issue

I am testing curl, and very new to this language.

let me explain what I m doing.

"http://somewebsite.com/click?param1=10&param2=523" this is the url which I am hitting in the browser and using Inspect Element and I got the following curl bash value --

curl 'http://somewebsite.com/click?param1=10&param2=523' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9' -H 'Cookie: pdval=9bc5d1fa982ff4c1e1f3d224' --compressed

Now here every time I hit that url in the browser, the value of parameter "pdval" is changing.

Is there any option to read the -H values in a bash script in Linux using curl.

Any help will be great. Thank you.


Solution

Since pdval is in cookie, you can make use of -b and -c options for cookie related task.

-c, --cookie-jar < filename >

(HTTP) Specify to which file you want curl to write all cookies after a completed operation. Curl writes all cookies from its in-memory cookie storage to the given file at the end of operations. 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.

-b, --cookie < name=data >

(HTTP) Pass the data to the HTTP server in the Cookie header. 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 argument, it is instead treated as a filename to read previously stored cookie from.

So if you set the -c option, then curl will automatically stores the cookie in a file. And you have to use -b to tell curl that take cookies from that file.

So you command should be as follows:

curl 'http://somewebsite.com/click?param1=10&param2=523' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9' -b /tmp/somewebsite.cookie -c /tmp/somewebsite.cookie --compressed


Answered By - Prashant Pokhriyal
Answer Checked By - David Marino (WPSolving Volunteer)