8.4
200 OK
GETの場合
リクエスト
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 /test HTTP/1.1
Host: example.jp
Content-Type: text/plain; charset=utf-8
こんにちは!
レスポンス
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
こんにちは!
201 Created
POSTの場合
レスポンスメッセージのLocationヘッダに絶対URIを入れる
リクエスト
POST /list HTTP/1.1
Host: example.jp
Content-type: text/pain; charset=utf-8
こんにちは!
レスポンス
HTTP/1.1 201 Created
Location: http://example.jp/list/item1
Content-Type: text/pain; charset=utf-8
こんにちは!
PUTの場合
クライアントが指定しているので、Locationヘッダは入らない
リクエスト
PUT /newitem HTTP/1.1
Host: example.jp
Content-type: text/pain; charset=utf-8
こんにちは!
レスポンス
HTTP/1.1 201 Created
Content-Type: text/pain; charset=utf-8
こんにちは!
301 Moved Permanently
feedがhttp://example.jp/oldfeed
からhttp://example.jp/newfeed
へと移動している場合
リクエスト
GET /oldfeed HTTP/1.1
Host: example.jp
レスポンス
HTTP/1.1 301 Moved Permanently
Location: http://example.jp/newfeed
Content-type: application/xhtml+xml; charset=utf-8
(移動を示す内容)
リクエスト
GET /newfeed HTTP/1.1
Host: example.jp
レスポンス
HTTP/1.1 200 OK
Content-type: application/atom+xml; charset=utf-8
<feed xmlns="http://www.w3.org/2005/Atom">
…
</feed>
303 See Other
リクエスト
POST /login HTTP/1.1
Host: example.jp
Content-type: application/x-www-form-urlencoded
username=yohei&password=foobar
レスポンス
HTTP/1.1 303 See Other
Location: http://example.jp/home/yohei
(別のURIを見るように示す内容)
リクエスト
GET /home/yohei HTTP/1.1
Host: example.jp
レスポンス
HTTP/1.1 200 OK
Content-type: application/xhtml+xml; charset=utf-8
<feed xmlns="http://www.w3.org/1999/xhtml">
…
</feed>
400 Bad Request
設定したパスワードが単純すぎるためにエラーを返したい場合
リクエスト
PUT /user/yohei HTTP/1.1
Content-Type: application/json
{
"name": "YAMAMOTO YOHEI",
"password": "foobar"
}
レスポンス
HTTP/1.1 400 Bad Request
Content-Type: application/json
{
"message": "パスワードが単純すぎます"
}
401 Unauthorized
リクエスト
DELETE /test HTTP/1.1
Host: example.jp
レスポンス
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Exampl.jp"
404 Not Found
リクエスト
GET /test HTTP/1.1
Host: example.jp
レスポンス
HTTO/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
リクエスト
GET /foo HTTP/1.1
Host: example.jp
レスポンス
HTTP/1.1 503 Service Unavailable
Content-Type: text/plain; charset=utf-8
Retty-After:3600
ただいまメンテナンス中です。
8.5
404 Not Foundの場合は下記のようにHTMLを返す
リクエスト
GET /foo HTTP/1.1
Host: example.jp
レスポンス
HTTP/1.1 404 Not found
Content-Type: application/xhtml+xml; charset=utf-8
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>エラー</title></head>
<body>ご指定のページは見つかりませんでした。</body>
</html>
8.6
- 例:エラーを
200 OK
で返してしまう場合
リクエスト
GET /test HTTP/1.1
Host: api.example.jp
レスポンス
HTTP/1.1 200 OK
Content-Type: application/xml
<error>
<code>1001</code>
<message>file not found</message>
</error>
- ファイル形式がわからないというエラーをここではXML形式で返す
- XML形式を知らないクライアントはこのレスポンスを正常の結果として扱ってしまう
- このエラーをWebAPI側の意図通りに処理するには専用のクライアントを作らねばならず、色々なクライアントで利用できるというWebAPIの利点が損なわれる
- Webサービスの場合でも同様