I have 2 pages: xyz.com/a
and xyz.com/b
. I can only access xyz.com/b
if and only if I login to xyz.com/a
first. If accessing xyz.com/b
without going through the other, I simply get access denied (no redirect to login) via the browser. Once I login at xyz.com/a
, I can access the other.
My problem is doing this using the curl command. I can login successfully to xyz.com/a
using curl, but then try xyx.com/b
and I get access denied.
I use the following:
curl --user user:pass https://xyz.com/a #works ok
curl https://xyz.com/b #doesn't work
I've tried using the second line with & without the user/password part and still doesn't work. Both pages uses the same CA, so that's not a problem. Any suggestions? Thanks
This question is related to
linux
bash
cookies
curl
http-authentication
After some googling I found this:
curl -c cookie.txt -d "LoginName=someuser" -d "password=somepass" https://oursite/a
curl -b cookie.txt https://oursite/b
No idea if it works, but it might lead you in the right direction.
My answer is a mod of some prior answers from @JoeMills and @user.
Get a cURL
command to log into server:
Modify cURL command to be able to save session cookie after login
-H 'Cookie: <somestuff>'
curl
at beginning -c login_cookie.txt
'login_cookie.txt'
in the same folderCall a new web page using this new cookie that requires you to be logged in
curl -b login_cookie.txt <url_that_requires_log_in>
I have tried this on Ubuntu 20.04 and it works like a charm.
Also you might want to log in via browser and get the command with all headers including cookies:
Open the Network tab of Developer Tools, log in, navigate to the needed page, use "Copy as cURL".
Source: Stackoverflow.com