LoginSignup
85
80

More than 3 years have passed since last update.

【curl】超入門(GET/POST/PUT/DELETEでリクエスト)[LINUX]

Last updated at Posted at 2018-12-29

はじめに

今回の記事はcurlコマンドについてです。
普段利用しているオプションをメインに簡単にまとめていきます。

curlを利用する前に

curlコマンドによる出力結果は左上から右下へとぎっしりと詰まっている表示となっております。
jsonなどでデータをレスポンスとして受け取る際など、直感的に理解しやすい構造で表示されていると嬉しいですよね。

そこで、それを可能にしてくれるjqというツールをインストールします。
MacOSではHomebrewでインストールします。
Mac以外のOSを利用している方は、こちらからコマンドをご確認いただけます。

$ brew install jq

jqを利用しない場合

$ curl http://localhost:3000/api/v1/articles/4
{"status":"SUCCESS","message":"Loaded article","data":{"id":4,"title":"The Cricket on the Hearth","body":"Cum illo rerum enim.","created_at":"2018-12-28T10:17:58.000Z","updated_at":"2018-12-28T10:17:58.000Z"}}

jqを利用した場合

$ curl http://localhost:3000/api/v1/articles/4 | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   209    0   209    0     0    902      0 --:--:-- --:--:-- --:--:--   904
{
  "status": "SUCCESS",
  "message": "Loaded article",
  "data": {
    "id": 4,
    "title": "The Cricket on the Hearth",
    "body": "Cum illo rerum enim.",
    "created_at": "2018-12-28T10:17:58.000Z",
    "updated_at": "2018-12-28T10:17:58.000Z"
  }
}

HTTPメソッド

curlコマンドが重宝される所以には、コマンドラインから簡単にリクエストすることができるということがあります。ここでは、GET、POST、PUT、DELETEメソッドを取り上げます。

メソッドはXオプションで指定します。

GET

GETリクエストの際はXオプションを省略することができます。

get.
$ curl http://localhost:3000/api/v1/articles/4 | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   209    0   209    0     0    902      0 --:--:-- --:--:-- --:--:--   904
{
  "status": "SUCCESS",
  "message": "Loaded article",
  "data": {
    "id": 4,
    "title": "The Cricket on the Hearth",
    "body": "Cum illo rerum enim.",
    "created_at": "2018-12-28T10:17:58.000Z",
    "updated_at": "2018-12-28T10:17:58.000Z"
  }
}

POST

データ(パラメータ)とともにリクエストをする際は、d(data)オプションでデータを記述できます。
-dもしくは--dataの後にクォーテーションで指定できます。複数パラメータがある場合は、&を利用します。

post.
$ curl http://localhost:3000/api/v1/articles -XPOST -d 'title=posted title&body=posted body' | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   222    0   187  100    35    859    160 --:--:-- --:--:-- --:--:--   861
{
  "status": "SUCCESS",
  "message": "Saved article",
  "data": {
    "id": 6,
    "title": "posted title",
    "body": "posted body",
    "created_at": "2018-12-29T03:46:19.000Z",
    "updated_at": "2018-12-29T03:46:19.000Z"
  }
}

PUT

put.
$ curl http://localhost:3000/api/v1/articles/7 -XPUT -d 'title=updated title&body=updated body' | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   228    0   191  100    37    869    168 --:--:-- --:--:-- --:--:--   872
{
  "status": "SUCCESS",
  "message": "Updated article",
  "data": {
    "id": 7,
    "title": "updated title",
    "body": "updated body",
    "created_at": "2018-12-28T16:11:34.000Z",
    "updated_at": "2018-12-29T03:44:14.000Z"
  }
}

DELETE

$ curl http://localhost:3000/api/v1/articles/7 -XDELETE | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   191    0   191    0     0    844      0 --:--:-- --:--:-- --:--:--   845
{
  "status": "SECCESS",
  "message": "Deleted article",
  "data": {
    "id": 7,
    "title": "updated title",
    "body": "updated body",
    "created_at": "2018-12-28T16:14:55.000Z",
    "updated_at": "2018-12-29T03:43:45.000Z"
  }
}

その他のオプション

oオプション

oオプションでファイル名を指定してレスポンスデータを出力できます。
GET、POST、PUT、DELETE全てのメソッドに対応しています。

o-option.
$ curl http://localhost:3000/api/v1/articles/4 -XGET -o response

$ less response | jq
{
  "status": "SUCCESS",
  "message": "Loaded article",
  "data": {
    "id": 4,
    "title": "The Cricket on the Hearth",
    "body": "Cum illo rerum enim.",
    "created_at": "2018-12-28T10:17:58.000Z",
    "updated_at": "2018-12-28T10:17:58.000Z"
  }
}

Oオプション

大文字のOオプションだとリクエスト先の名前でファイルを保存します。

O-option.
$ curl http://localhost:3000/api/v1/articles/4 -XDELETE -O

$ less 4 | jq
{
  "status": "SUCCESS",
  "message": "Deleted article",
  "data": {
    "id": 4,
    "title": "The Cricket on the Hearth",
    "body": "Cum illo rerum enim.",
    "created_at": "2018-12-28T10:17:58.000Z",
    "updated_at": "2018-12-28T10:17:58.000Z"
  }
}

Iオプション

レスポンスのヘッダーのみを取得して出力します。

I-option.
$ curl http://localhost:3000/api/v1/articles/3 -I
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
ETag: W/"ed33def96c3d8311d744c5cc3314c580"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: ac467788-6317-46fd-863b-aad41ae4ff61
X-Runtime: 0.005535

iオプション

レスポンスのヘッダー、ボディー両方を取得して出力します。

i-option.
$ curl http://localhost:3000/api/v1/articles/3 -i
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
ETag: W/"ed33def96c3d8311d744c5cc3314c580"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 57ffad86-81e4-4420-9026-057fe5949cc4
X-Runtime: 0.005409
Transfer-Encoding: chunked

{"status":"SUCCESS","message":"Loaded article","data":{"id":3,"title":"The World, the Flesh and the Devil","body":"Soluta doloribus magni praesentium.","created_at":"2018-12-28T10:17:58.000Z","updated_at":"2018-12-28T10:17:58.000Z"}}

vオプション

リクエストのヘッダー、レスポンスのヘッダー、ボディー両方を取得して出力します。

v-option.
$ curl http://localhost:3000/api/v1/articles/3 -v | jq
  % 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 ::1...
* TCP_NODELAY set
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /api/v1/articles/3 HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< ETag: W/"ed33def96c3d8311d744c5cc3314c580"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: 862ae6dd-bece-4258-b398-58691957fcfc
< X-Runtime: 0.006274
< Transfer-Encoding: chunked
<
{ [244 bytes data]
100   233    0   233    0     0   1070      0 --:--:-- --:--:-- --:--:--  1073
* Connection #0 to host localhost left intact
{
  "status": "SUCCESS",
  "message": "Loaded article",
  "data": {
    "id": 3,
    "title": "The World, the Flesh and the Devil",
    "body": "Soluta doloribus magni praesentium.",
    "created_at": "2018-12-28T10:17:58.000Z",
    "updated_at": "2018-12-28T10:17:58.000Z"
  }
}

おわりに

その他のコマンドと同様、curlにも便利なオプションがたくさん用意されています。今回ご紹介したオプションはほんの一部ではありますが、僕が普段主に利用しているものです。
僕自身ももっと幅広くオプションを使いこなせるよう勉強していきたいと思います。

こちらの記事が役に立ったという方は、いいね、よろしくお願いします(^^)

85
80
1

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
85
80