#author("2022-03-13T05:38:28+00:00","default:hotate","hotate")
#author("2023-10-22T08:55:23+00:00;2022-03-13T05:38:28+00:00","default:hotate","hotate")
#contents
&tag(curl, HTTP, コマンド);
&tag(curl,HTTP,コマンド);

* 情報 [#n2806dc9]
** 概要 [#ta01664b]
- [[cURL - Manual (マニュアル日本語訳)>http://www.hcn.zaq.ne.jp/___/unix/curl_manual.html]]
 ウェブページを取得して、ローカルファイルに名前を付けて保存:
 curl -o thatpage.html http://www.netscape.com/
-- POST
 curl によるデータのポストは簡単である。 -d データ オプションを用いる。 ポストデータは URL エンコードしておかなければならない。
 簡単な name と phone のポスト:
 curl -d "name=Rafael%20Sagula&phone=3320780" http://www.where.com/guest.cgi

** オプション [#m80dd7cb]
- [[よく使うcurlコマンドのオプションまとめ(12個) - Qiita>https://qiita.com/shtnkgm/items/45b4cd274fa813d29539]]
 # NTLM認証もしくはDigest認証のかかったサイトにアクセス(--anyauth)
 $ curl --anyauth --user USERID:PASSWORD http://www.example.com/
- [[curl option 覚え書き - Qiita>https://qiita.com/takayukioda/items/edf371b3566bea64d046]]
 curl コマンド、ちょくちょく使うけどオプション忘れてまう
- [[理解しておきたいcurlの --write-out オプション>https://zenn.dev/taxin/articles/curl-time-measure]] (2021/01/21)
 curlのversion7.70.0からは、フォーマットの指定部分で -w '%{json}' のようにして取得できる全ての項目をjson形式で出力できます。

** ファイル [#cb833002]
- [[curl コマンド 使い方メモ - Qiita>http://qiita.com/yasuhiroki/items/a569d3371a66e365316f]]
 -O だと、リクエスト先の名前でファイルを保存します。
- [[curlでファイルをダウンロードする - ksaitoの日記>http://d.hatena.ne.jp/ksaito11/20090605/1244208059]]
 -Lがリダイレクトに対応、-Oがファイルに保存(指定しないと標準出力です。)
 $ curl -L -O [URL]

** POST [#f6673d7a]
- [[curl を使って JSONデータをPOSTする - // Takuya71 の日記>http://takuya71.hatenablog.com/entry/2012/11/10/143415]]
 % curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"user":{"first_name":"first name", ...}}'  http://localhost:3000/api/1/users
- [[curl と cron でHTTPレスポンスを監視するシェルスクリプト (結果をSlack に飛ばす) - Qiita>https://qiita.com/Kakimoty_Field/items/754df3a181187fbb517c]]
 インターネットに出られるけれど、インターネットから入ってこれないサーバの死活監視を、お気軽にしたい。

** 測定 [#z8ef68fa]
- [[cURLでHTTPステータスコードだけを取得する - Qiita>http://qiita.com/mazgi/items/585348b6cdff3e320726]]
 $ curl -LI mazgi.com -o /dev/null -w '%{http_code}\n' -s
- [[cURL で取得できるレスポンスタイムの内訳 - 元RX-7乗りの適当な日々>https://www.na3.jp/entry/20201228/p1]]
 curl -L --output /dev/null --silent --show-error --write-out 'lookup:        %{time_namelookup}\nconnect:       %{time_connect}\nappconnect:    %{time_appconnect}\npretransfer:   %{time_pretransfer}\nredirect:      %{time_redirect}\nstarttransfer: %{time_starttransfer}\ntotal:         %{time_total}\n' 'google.com'
- [[curlでパフォーマンス測定 | DevelopersIO>https://dev.classmethod.jp/articles/curl-benchmark/]] (2021/6/1)
 コマンドラインツールのcurlを用いてHTTPによる通信のパフォーマンスを調べる方法を考えていこうと思います。

** [[スクレイピング]] [#a9179714]
- [[curl+xpath から始めるお手軽スクレイピング(2) - それマグで!>https://takuya-1st.hatenablog.jp/entry/2015/12/18/curl%2Bxpath]]
 curl でも、連続したページの取得ができます。それが --next オプションです。
 curl に--config/-K オプションをつけると連続してデータ取得をバッチ処理してくれます。
- [[Linux/Macのシェルを使ってrssの分解をする方法 - ジャンカーな日々>http://dorodorodoroid.hatenablog.com/entry/2015/01/27/075858]]
 たとえば、RSSを取得してタイトルと要素を引っ張り出す場合は
 cat /rss/channel/item[0]/title" | xmllint --shell "抽出したいxmlファイル
- [[気象庁防災XMLのPull型提供(Atom Feed)について - Qiita>https://qiita.com/e_toyoda/items/185c1dea055e230918b9]]
 最低限の取得スクリプト例
 わかりやすいようにシェルスクリプトです。長時間試験していません。

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS