ステータスコードとその中身 (HTTP1.1)
ステータスコードの概要とよく見るステータスコードの意味とHTTP通信の中身について調べてまとめました。
エンジニアでなくても、404 NOT FOUND
は、見たことがある方も多いと思います。
エンジニアならば、ステータスコードを見て瞬時に取るべき手段を判断できるようになるといいですね。
ざっくり5つの分類
RFC2616の定義では、ステータスコードは先頭の数字によって5つに分類される。
1xx: 処理中
処理が継続していることを示す。
2xx: 成功
リクエストが成功したことを示す。
3xx: リダイレクト
ほかのリソースへのリダイレクトを示す。 クライアントは3xxを受け取ったとき、レスポンスメッセージのLocationヘッダをみて新しいリソースを接続する
4xx: クライアントエラー
クライアントのリクエストによるエラー
5xx: サーバエラー
サーバ側に原因があるエラー
よく見るステータスコード
200 OK - リクエスト成功
リクエスト
GET /test HTTP/1.1 Host: example.jp
レスポンス
HTTP/1.1 200 OK Content-Type: text/plain; charset=utf-8 Hello, World!
PUTやPOSTはボディに処理結果が入る。
リクエスト (PUT)
PUT /test HTTP1.1 Host: example.jp Content-Type: text/plain; charset=utf-8 Hello, World!
レスポンス
HTTP/1.1 200 OK Content-Type: text/plain; charset-utf-8 Hello, World!
201 Created - リソースの作成成功
リソースを新しく作成したことを示す。
POST, PUTのレスポンスとして返される。
POSTの場合
新しく作成したリソースのURIはレスポンスのLocationヘッダに絶対URIとして入る
リクエスト (POST)
POST /list HTTP/1.1 Host: example.jp Content-Type: text/plain; charset=utf-8 Hello, World!
レスポンス
HTTP/1.1 201 Created Location: http://example.jp/list/item1 //新しく作成したリソースのURI Content-Type: text/plain; charset=utf-8 Hello, World!
PUTの場合
クライアントが新しいリソースのURIを知っているため、Locationヘッダには入らない
リクエスト (PUT)
PUT /newitem HTTP/1.1 Host: exapmle.jp Content-Type: text/plain; charset=utf-8 Hello, World!
レスポンス
HTTP/1.1 201 Created Content-Type: text/plain; charset=utf-8 Hello, World!
301 Moved Permanently - リソースの恒久的な移動 (リダイレクト)
リクエストで指定したリソースが新しいURIに移動したことを示す。
新しいURLはレスポンスのLocationヘッダに絶対URLとして入る。
リクエスト
GET /oldfeed HTTP/1.1 Host: exapmle.jp
レスポンス
HTTP/1.1 301 Moved Permanently Location: http://example.jp/newfeed Content-Type; application/xhtml+xml; charset=utf-8 <html> <head></head> <body></body> </html>
リクエスト
GET /newfeed HTTP/1.1 Host: example.jp
レスポンス
HTTP/1.1 200 OK Content-Type: application/atom+xml; charset=utf8 <feed cjohs="https://www.w3.org/2005/Atom"> ... </feed>
303 See Other - 別URIの参照
リクエストに対する処理結果が別のURIで取得できることを示す。
POSTした結果をGETで取得するときなどに使う。
リクエスト (POST)
POST /login HTTP/1.1 Host: example.jp Content-Type: application/x-www-form-urlencoded username=...&password=...
レスポンス
HTTP/1.1 303 See Other Location: http://example.jp/home/... Content-Type: application/xhtml+xml; charset=utf-8 <html xmlns="http://www.w3.org/1000/xhtml"> <head></head> <body> </body> </html>
リクエスト
GET /home/... HTTP/1.1 Host: example.jp
レスポンス
HTTP/1.1 200 OK Content-Type: appliication/xhtml+xml; charset=utf-8 <html xmlns="http://www.w3.org/1999/xhtml"> ... </html>
400 Bad Request - リクエストの間違い
リクエストの構文やパラメータのミスを示す。
リクエスト (POST)
PUT /user/... HTTP/1.1 Content-Type: application/json { "name": "Ryutaro Fukuma", "password": "xxxx" }
レスポンス
HTTP/1.1 400 Bad Request Content-Type: application/json { "message": "パスワードが単純すぎます" }
401 Unauthorized - アクセス権不正
適切な認証情報を与えずにリクエストを行ったことを示す。
レスポンスのWWW-Authenticateヘッダで、クライアントに対して認証方式を伝える。
リクエスト (DELETE)
DELETE /test HTTP/1.1 Host: example.jp
レスポンス
HTTP/1.1 401 Unauthorized WWW-Authenticate: Basic realm="example.jp"
404 Not Found - リソースの不在
指定したリソースが見つからないことを示す。
レスポンスボディにはその理由が入る。
リクエスト
GET /test HTTP1.1 Host: example.jp
レスポンス
HTTP/1.1 404 Not Found Content-Type: text/plain; charset=utf-8 http://example.jp/testは見つかりませんでした。
500 Internal Server Error - サーバ内部のエラー
サーバ側に何らかの異常が生じ、正しいレスポンスが返せないことを示す。
レスポンスボディにその理由が入る。
他に適切なサーバエラーを示すステータスコードがない場合にも用いられる。
リクエスト
GET /foo HTTP/1.1 Host: example.jp
レスポンス
HTTP/1.1 500 Internal Server Error Content-Type: text/plain; charset=utf-8 サーバに異常が起きています。しばらく経ってから再度アクセスしてください。
503 Service Unavailable - サービス停止
サーバがメンテナンスなどで一時的にアクセスできないことを示す。
レスポンスボディにその理由が入る。
レスポンスのRetry-Afterヘッダでサービス再開時期がおよそ何秒後であるかを通知できる。
リクエスト
GET /foo HTTP/1.1 Host: example.jp
レスポンス
HTTP/1.1 503 Service Unavailable Content-Type: text/plain; charset=utf-8 Retry-After: 3600 ただいまメンテナンス中です。しばらく経ってから再度アクセスしてください。