CURL to access a page that requires a login from a different page CURL to access a page that requires a login from a different page bash bash

CURL to access a page that requires a login from a different page


The web site likely uses cookies to store your session information. When you run

curl --user user:pass https://xyz.com/a  #works okcurl https://xyz.com/b #doesn't work

curl is run twice, in two separate sessions. Thus when the second command runs, the cookies set by the 1st command are not available; it's just as if you logged in to page a in one browser session, and tried to access page b in a different one.

What you need to do is save the cookies created by the first command:

curl --user user:pass --cookie-jar ./somefile https://xyz.com/a

and then read them back in when running the second:

curl --cookie ./somefile https://xyz.com/b

Alternatively you can try downloading both files in the same command, which I think will use the same cookies.


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".

screenshot


After some googling I found this:

curl -c cookie.txt -d "LoginName=someuser" -d "password=somepass" https://oursite/acurl -b cookie.txt https://oursite/b

No idea if it works, but it might lead you in the right direction.