justdan0227
justdan0227

Reputation: 1362

CURL how to POST and then GET

How do I issue a POST to authenticate to a site followed by a GET to retrieve data all from the command line - MAC

This does not work

curl -X POST -d '{"username":"xyz","password":"xyz"}' -H "Content-Type: application/json" https://example.com/login
curl -X GET -H "Content-Type: application/json" https://example.com/getdata

Upvotes: 0

Views: 2645

Answers (1)

Daniel Stenberg
Daniel Stenberg

Reputation: 58034

Avoid using -X, that causes more problem than it solves. By default, curl uses the implied method based on the options you provide.

The key is often to store the cookies you get back (with -c). That's your "session". And often you need to follow redirects on the login action (with -L).

In the second request you then load the cookies that were stored in the first command (with -b).

Your command lines with these changes then end up like this:

curl -d '{"username":"xyz","password":"xyz"}' -H "Content-Type: application/json" https://example.com/login -c cookies.txt -L
curl -H "Content-Type: application/json" https://example.com/getdata -b cookies.txt

Upvotes: 2

Related Questions