私は私のサーバー(usign Javaと春)に画像を掲示しています。
物事のカップルが発生する可能性があります:
- すべてがうまくいけば、私は返す
200
と画像ID。 - 画像は、(ファイルシステムに保存することはできません
SaveFileException
)。 - データベースは新しいURLで新しいイメージのレコードを更新することはできません(
DatabaseException
)。 - または投げます
IOException
。
要するに、私はイメージを作成/保存することはできません。
私は何HTTPコードを返す必要がありますか?
私は複数のコードとに応じて複数のメッセージを返す必要がありますかException
?
まず第一に、私はステータスコードを理解し、クライアントの要求を満たすためにサーバの試みの結果を示すことを意味していることを強調表示してみましょう。
すべてがうまくいけば、私は返す
200
と画像ID。
罰金のようですが、私はあなたが返すように助言する201
とともにLocation
代わりのヘッダー。引用RFC 7231に関するPOST
方法:
1つ以上のリソースが正常に処理の結果、オリジンサーバ上に作成されている場合
POST
、要求を、オリジンサーバが送るべき201
含む(作成)応答Location
作成プライマリ・リソースの識別子を提供するヘッダ・フィールドと説明表現を新しいリソース(複数可)を参照しながら要求の状態。
一緒に201
、Location
ヘッダは、新しく作成されたリソースがある場所を示すことを意味します。いかなる場合Location
ヘッダが設けられていない場合、クライアントは、リソースが有効なリクエストURIによって識別されると仮定すべきです。
201
(作成した)ステータスコードは、要求が満たされた1つ以上の新しいリソースが作成さをもたらしていることを示しています。要求によって作成されたプライマリ・リソースのいずれかによって識別されLocation
ない場合、応答のヘッダフィールドまたはLocation
フィールドが有効な要求URIによって、受信されません。[...]
クライアントのエラー
クライアントは、新しい要求を実行し、問題を解決することはできますか?もしそうなら、中にステータスコードを選択4xx
範囲:
4xx
ステータスコードの(クライアントエラー)クラスは、クライアントが誤りを犯しているように見えることを示しています。応答するときを除きHEAD
、要求、サーバはエラー状況の説明を含む表現を送るべきで、それが一時的または恒久的な条件があるかどうか。これらのステータスコードはどんなリクエストメソッドに適用されます。
マイケルKropatは非常に便利なまとめフローチャートの一連のあなたにいくつかのinsighstsを与える可能性があります。最も適切かを決定するために、次の表を参照してください。4xx
ステータスコードを:
いくつかの有効なオプションは、エラーの原因に応じて、以下のとおりです。
The
413
(Payload Too Large) status code indicates that the server is refusing to process a request because the request payload is larger than the server is willing or able to process. [...]
6.5.13. 415 Unsupported Media Type
The
415
(Unsupported Media Type) status code indicates that the origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource. The format problem might be due to the request's indicatedContent-Type
orContent-Encoding
, or as a result of inspecting the data directly.
The
400
(Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
Server error
If the error was caused by the server, then a status code in the 5xx
range will be accurate:
The
5xx
(Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method. Except when responding to aHEAD
request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.
See the following flowchart:
I would suggest 500
:
500
(内部サーバーエラー)ステータスコードサーバーが要求を満たすことを妨げる予期しない状態に遭遇したことを示しています。