LoginSignup
29
33

More than 3 years have passed since last update.

curl入門 - GET編

Posted at

はじめに

curlはCLIでHTTPリクエストなどを実行するためのコマンドです。
curl公式HP

いろいろできるcurlですが、今回はHTTPリクエストのGETに絞って書いていきます。
仕事でAPIレスポンスを確認する作業があったので、それの備忘録も兼ねてます。

コマンドの挙動はShangriLaAnime API Serverを使わせていただきながら確認しました。

基本コマンド

まずは2019年3期のアニメタイトルを取得してみます。
やり方は、curlコマンドの引数にAPIのリクエストURLを持たすだけ。

curl 'http://api.moemoe.tokyo/anime/v1/master/2019/3'

以下はレスポンス。長いので途中以降は省略してます。

[{"id":972,"title":"アグレッシブ烈子 シーズン2","title_short1":"アグレッシブ烈子","title_short2":"","title_short3":"","public_url":"https://www.netflix.com/jp/title/80198505","twitter_account":"retsuko_sanrio","twitter_hash_tag":"アグレッシブ烈子","cours_id":0,"created_at":"2019-06-30T14:03:33Z","updated_at":"2019-06-30T14:03:33Z","sex":0,"sequel":0,"city_code":0,"city_name":"","product_companies":"ファンワークス"},{"id":973,"title":"7SEEDS","title_short1":"7SEEDS","title_short2":"","title_short3":"","public_url":"http://7seeds.jp/","twitter_account":"7SEEDS_anime","twitter_hash_tag":"7seeds","cours_id":0,"created_at":"2019-06-30T14:03:33Z","updated_at":"2019-06-30T14:03:33Z","sex":0,"sequel":0,"city_code":0,"city_name":"","product_companies":"GONZO"},
...続く
]

返ってきた。簡単ですね。

jqを使う

curlだけだとレスポンスが平文なので、jsonを整形してくれるjqというライブラリを使います。

まずはHomebrewでインストール。

brew install jq

Homebrewを使用してない場合は、jqのダウンロードページを確認してください。

インストールできたら、curlコマンドの末にjqコマンドを追加します。

curl 'http://api.moemoe.tokyo/anime/v1/master/2019/3' | jq .

jq ..はrootの意味で、要するにレスポンス全部整形するよ、という感じです。
ここをjq '.items.name'とか様々な指定の仕方ができるんですが、詳細は公式HPや他記事をご参考ください。

※参考:jq コマンドを使う日常のご紹介 - Qiita

以下レスポンスです。

[
  {
    "id": 972,
    "title": "アグレッシブ烈子 シーズン2",
    "title_short1": "アグレッシブ烈子",
    "title_short2": "",
    "title_short3": "",
    "public_url": "https://www.netflix.com/jp/title/80198505",
    "twitter_account": "retsuko_sanrio",
    "twitter_hash_tag": "アグレッシブ烈子",
    "cours_id": 0,
    "created_at": "2019-06-30T14:03:33Z",
    "updated_at": "2019-06-30T14:03:33Z",
    "sex": 0,
    "sequel": 0,
    "city_code": 0,
    "city_name": "",
    "product_companies": "ファンワークス"
  },
  {
    "id": 973,
    "title": "7SEEDS",
    "title_short1": "7SEEDS",
    "title_short2": "",
    "title_short3": "",
    "public_url": "http://7seeds.jp/",
    "twitter_account": "7SEEDS_anime",
    "twitter_hash_tag": "7seeds",
    "cours_id": 0,
    "created_at": "2019-06-30T14:03:33Z",
    "updated_at": "2019-06-30T14:03:33Z",
    "sex": 0,
    "sequel": 0,
    "city_code": 0,
    "city_name": "",
    "product_companies": "GONZO"
  },
 ...続く
]

無事返ってきました。見やすいですね。

ファイルに出力する

レスポンスが多い場合、CLIの画面では見づらいことがあります。
なのでファイルに出力してレスポンスデータを操作しやすくしてみましょう。

使いオプションは-oです。

curl 'http://api.moemoe.tokyo/anime/v1/master/2019/3' -o response

これでresponseという名前のファイルにレスポンスが書き出されました。(ファイルは無ければ作成されます)
エディタ等で操作できて便利ですね。

まとめ

GETは、以下の書き方すればOK。必要によってjqはつけ外ししましょう。

curl 'http://api.moemoe.tokyo/anime/v1/master/2019/3' | jq .

他にもオプションがたくさんあるので、公式HPやcurl コマンド 使い方メモ - Qiitaを参考にしてみてください。

参考

curl公式HP
jq公式HP
curl コマンド 使い方メモ - Qiita
jqコマンドでjsonを扱う - Qiita
ShangriLaAnime API Server

29
33
0

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
29
33