はじめに
curl は、CLIベースの HTTPクライアントとして有名ですね。HTTPの仕組みとして、リダイレクトされた場合には、300番台のステータスコードが返ってきますが、勝手にリダイレクト先のアドレスにはアクセスしてくれません。
root@22718a86e84c:/share# curl -v http://google.com * Rebuilt URL to: http://google.com/ * Trying 172.217.31.174... * Connected to google.com (172.217.31.174) port 80 (#0) > GET / HTTP/1.1 > Host: google.com > User-Agent: curl/7.47.0 > Accept: */* > < HTTP/1.1 302 Found < Cache-Control: private < Content-Type: text/html; charset=UTF-8 < Referrer-Policy: no-referrer < Location: http://www.google.co.jp/?gfe_rd=cr&dcr=0&ei=bKttWoeaFqvB8wfwrKDwDQ < Content-Length: 271 < Date: Sun, 28 Jan 2018 10:52:28 GMT < <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>302 Moved</TITLE></HEAD><BODY> <H1>302 Moved</H1> The document has moved <A HREF="http://www.google.co.jp/?gfe_rd=cr&dcr=0&ei=bKttWoeaFqvB8wfwrKDwDQ">here</A>. </BODY></HTML> * Connection #0 to host google.com left intact
これを、自動でリダイレクトさせるオプションがあったのでメモです。
-L:リダイレクトさせるオプション
-L
オプションを使うとリダイレクトしてくれます。
root@22718a86e84c:/share# curl -v -L http://google.com * Rebuilt URL to: http://google.com/ % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 172.217.31.174... * Connected to google.com (172.217.31.174) port 80 (#0) > GET / HTTP/1.1 > Host: google.com > User-Agent: curl/7.47.0 > Accept: */* > < HTTP/1.1 302 Found < Cache-Control: private < Content-Type: text/html; charset=UTF-8 < Referrer-Policy: no-referrer < Location: http://www.google.co.jp/?gfe_rd=cr&dcr=0&ei=Tq1tWsXCMqzB8wfZiJyQDg < Content-Length: 271 < Date: Sun, 28 Jan 2018 11:00:30 GMT < * Ignoring the response-body { [271 bytes data] 100 271 100 271 0 0 9374 0 --:--:-- --:--:-- --:--:-- 9678 * Connection #0 to host google.com left intact * Issue another request to this URL: 'http://www.google.co.jp/?gfe_rd=cr&dcr=0&ei=Tq1tWsXCMqzB8wfZiJyQDg' * Trying 172.217.27.163... * Connected to www.google.co.jp (172.217.27.163) port 80 (#1) > GET /?gfe_rd=cr&dcr=0&ei=Tq1tWsXCMqzB8wfZiJyQDg HTTP/1.1 > Host: www.google.co.jp > User-Agent: curl/7.47.0 > Accept: */* > < HTTP/1.1 200 OK < Date: Sun, 28 Jan 2018 11:00:30 GMT < Expires: -1 < Cache-Control: private, max-age=0 < Content-Type: text/html; charset=Shift_JIS < P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info." < Server: gws < X-XSS-Protection: 1; mode=block < X-Frame-Options: SAMEORIGIN < Set-Cookie: 1P_JAR=2018-01-28-11; expires=Tue, 27-Feb-2018 11:00:30 GMT; path=/; domain=.google.co.jp < Set-Cookie: NID=122=ZqDI7fbiq0GEYlX3-Y02ySoN5sW6eRkle3e77hyGW3Xz4Ub_44OTHrqqsk_p8I3aKuiBUTCI8ioaQD_QEpAT779wMPV4dlVH_CS61fVEKQgkGrZWbp58ZKIYzvwrrkgS; expires=Mon, 30-Jul-2018 11:00:30 GMT; path=/; domain=.google.co.jp; HttpOnly < Accept-Ranges: none < Vary: Accept-Encoding < Transfer-Encoding: chunked < { [764 bytes data] 100 11542 0 11542 0 0 80711 0 --:--:-- --:--:-- --:--:-- 80711 * Connection #1 to host www.google.co.jp left intact
おわりに
自動化するときに便利ですね。こんな感じで使えそうです。
root@22718a86e84c:/share# curl -s -L -I http://google.com | grep "200 OK" HTTP/1.1 200 OK