86
75

curlを使ってベーシック認証→ユーザーログイン→ファイルダウンロード

Last updated at Posted at 2016-06-29

目的

あるURLからファイルをダウンロードする。
ただし、ベーシック認証とログイン処理が必要。
※curlとはHTTPのGET/POSTやらをCUIで行うことができるコマンド(およびライブラリ)

curlを使った色々

ベーシック認証

ユーザー名:user
パスワード:password の場合

curl -u user:password http://example.com

パラメータを含むGET

パラメータ1: param1
値1: 123
パラメータ2: param2
値2: 345

curl http://example.com/hogehoge.cgi -d param1=123 -d param2=345

URLに?param1=123とかつなげることも可能だが、
複数パラメータの場合は & をエスケープでフォローしないといけない。

セッション保持

ログインするユーザー名:Abe
パスワード:ebA(ひどい)

curl -c cookie.txt http://example.com/login.cgi -d name=Abe password=ebA

ファイルのダウンロード

curl -L http://example.com/file.cgi -d name=hoge.bin -o hoge.bin

まとめ

ベーシック認証→ユーザーログイン→ファイルダウンロード
ユーザーログインのパラメータをPOSTした後、
cookieを保存しておいてからファイルをダウンロード。
成功すると、hoge.binのファイルが取得できる。

curl -c cookie.txt -u user:password http://example.com/login.cgi -d name=Abe password=ebA
curl -b cookie.txt -u user:password http://example.com/file.cgi -d name=hoge.bin -o hoge.bin
86
75
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
86
75