HTTP标头或响应正文中的休息错误消息? [英] Rest error message in HTTP Header or Response Body?

查看:138
本文介绍了HTTP标头或响应正文中的休息错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个暴露给iPhone和Android客户端的REST服务。目前我遵循HTTP代码200,400,401,403,404,409,500等。

I have a REST service that is exposed to iPhone and Android clients. Currently I follow the HTTP codes 200, 400, 401, 403, 404, 409, 500 etc.

我的问题是建议放置原因/描述的地方在哪里/错误原因? REST API是否更有意义总是在标题中包含自定义Reason?

My question is where is the recommended place to put the reason/description/cause of the error? Does it make more sense for the REST API to always have custom Reason in the header like so?

< HTTP/1.1 400 Bad Request - Missing Required Parameters.
< Date: Thu, 20 Dec 2012 01:09:06 GMT
< Server: Apache/2.2.22 (Ubuntu)
< Connection: close
< Transfer-Encoding: chunked

或者通过JSON将它放在Response Body中会更好吗?

Or is it better to have it in the Response Body via JSON?

< HTTP/1.1 400 Bad Request
< Date: Thu, 20 Dec 2012 01:09:06 GMT
< Server: Apache/2.2.22 (Ubuntu)
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: application/json
{ "error" : "Missing Required Parameters" }


推荐答案

引用400.x错误代码的HTTP规范:

Quoting from the HTTP specification for 400.x error codes:


4xx类状态代码适用于客户端
似乎有错误的情况。除了在响应HEAD请求时,
服务器应该包含一个实体,其中包含错误
情况的解释,以及它是暂时的还是永久的。这些
状态代码适用于任何请求方法。用户代理应该是
向用户显示任何包含的实体。

The 4xx class of status code is intended for cases in which the client seems to have erred. Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method. User agents SHOULD display any included entity to the user.

最佳做法是将错误消息包含为实体在HTTP响应的主体中 - 无论是JSON,纯文本,格式化的HTML,还是您可能想要使用的任何其他格式。

It is best practice to include the error message as an entity in the body of the HTTP response - be it JSON, plain text, formatted HTML, or whatever other format you may want to utilize.

这篇关于HTTP标头或响应正文中的休息错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆