curlコマンドで、ステータスコードとレスポンスボディだけを取得する
シェルスクリプトで、httpリクエストのレスポンスをアレコレする必要があったのさ。
このコマンドで実現可能
curl -o - http://google.com -w '%{http_code}\n' -s
実行結果
$ curl -o - http://google.com -w '%{http_code}\n' -s <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>301 Moved</TITLE></HEAD><BODY> <H1>301 Moved</H1> The document has moved <A HREF="http://www.google.com/">here</A>. </BODY></HTML> 301
レスポンスボディと、最終行に「301」とステータスコードが取得できてる!
-wオプションの%{http_code}便利。manページみたらほかにもいろいろあるっぽい。
あとはシェルスクリプト内でパースすればいいかんじになるね~\(^o^)/
パースについてはまた今度...
参考
ステータスコードのみを取得
$ curl -LI google.com -o /dev/null -w '%{http_code}\n' -s 200
レスポンスボディだけを取得(オプション指定しない場合)
$ curl google.com <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>301 Moved</TITLE></HEAD><BODY> <H1>301 Moved</H1> The document has moved <A HREF="http://www.google.com/">here</A>. </BODY></HTML>
全部取得(ステータスコード、ヘッダー、レスポンスボディ)
$ curl -Ssi google.com HTTP/1.1 301 Moved Permanently Location: http://www.google.com/ Content-Type: text/html; charset=UTF-8 Date: Thu, 16 Jan 2020 03:28:27 GMT Expires: Sat, 15 Feb 2020 03:28:27 GMT Cache-Control: public, max-age=2592000 Server: gws Content-Length: 219 X-XSS-Protection: 0 X-Frame-Options: SAMEORIGIN <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> <TITLE>301 Moved</TITLE></HEAD><BODY> <H1>301 Moved</H1> The document has moved <A HREF="http://www.google.com/">here</A>. </BODY></HTML>
参考リンク
これとか?
— Hiroki Mori@カメレオンエンジニア (@hrkmr_tech) 2020年1月16日
curl -o - "URL" -w ',%{http_code}'