2

I'm sending POST request to some link with valid username and password.

I use this command: curl -X POST -F 'username=test@me.com' -F 'password=123' http://my.site.com/login

but it got Moved Temporarily. Redirecting to /

How can I redirect with cURL ?

HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Sun, 09 Oct 2016 07:43:26 GMT
Content-Type: text/html
Content-Length: 58
Connection: close
Vary: Accept
Location: /
Set-Cookie: connect.sid=s%3A%2FU%2FG5YeSNkjkaA%2Fet4evdmgj.QIGEGtrJQxNcNx1bdF6QzshqsEXn3hys1V2mkyuw3nY; Path=/; Expires=Sun, 09 Oct 2016 08:13:26 GMT; HttpOnly

<p>Moved Temporarily. Redirecting to <a href="/">/</a></p>
2

curl's -L (--location) option follows a redirected URL:

curl -LX POST ...

But the caveat is that, this will follow redirections after login. If the login page redirects to another login page where you need to (re)submit the same POST request, then you need to use the --location-trusted option to make all the redirected URLs as trusted, otherwise curl will send the POST authentication info only to the initial URL. So if you trust the redirected URL, do:

curl -L --location-trusted -X POST ...
  • i must add / after -LX ? – Mehran Goudarzi Oct 9 '16 at 8:00
  • @MehranGoudarzi No, just do curl -LX POST -F 'username=test@me.com' -F 'password=123' http://my.site.com/login, if other things are correct then this should work. – heemayl Oct 9 '16 at 8:02
  • i did it but seems not login . i login with Chrome and see view source , that Different from curl show . – Mehran Goudarzi Oct 9 '16 at 8:07
  • @MehranGoudarzi Check my edits. – heemayl Oct 9 '16 at 8:09
  • @MehranGoudarzi Make sure the HTML directives are correct, otherwise this should work. – heemayl Oct 9 '16 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.