使用HTTP状态codeS反映的Web服务请求成功/失败? [英] Using HTTP status codes to reflect success/failure of Web service request?

查看:152
本文介绍了使用HTTP状态codeS反映的Web服务请求成功/失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我采取一个返回JSON-CN codeD有效载荷的Web服务。如果服务调用失败 - 说,由于无效的参数 - 返回一个JSON-CN codeD错误。我不能确定,不过,HTTP状态code应该在这种情况下返回。

I'm implementing a Web service that returns a JSON-encoded payload. If the service call fails -- say, due to invalid parameters -- a JSON-encoded error is returned. I'm unsure, however, what HTTP status code should be returned in that situation.

在一方面,它似乎像HTTP状态codeS是HTTP:即使是返回一个应用程序错误,在HTTP传输的自身的成功,显示了 200 OK 响应。

On one hand, it seems like HTTP status codes are for HTTP: even though an application error is being returned, the HTTP transfer itself was successful, suggesting a 200 OK response.

在另一方面,RESTful方式似乎表明,如果来电者试图张贴到资源,并且请求的JSON参数无效不知何故,一个 400错误的请求是适当的。

On the other hand, a RESTful approach would seem to suggest that if the caller is attempting to post to a resource, and the JSON parameters of the request are invalid somehow, that a 400 Bad Request is appropriate.

我使用在客户端的原型,它有一个很好的机制,自动调度到基于HTTP状态code(的onSuccess onFailure 的不同回调),所以我倾向于使用状态codeS指示服务成功或失败,但我很有兴趣听到的话,任何人有在这个问题上普遍的做法的意见和经验。

I'm using Prototype on the client side, which has a nice mechanism for automatically dispatching to different callbacks based on HTTP status code (onSuccess and onFailure), so I'm tempted to use status codes to indicate service success or failure, but I'd be interested to hear if anyone has opinions or experience with common practice in this matter.

谢谢!

推荐答案

http状态code只是用于指示应用程序响应的状态。 正如你所说,如果JSON参数,不知何故无效,400状态code 一个合适的答案。

http status code are just for indicating the status of the application response. and as you said, if json parameters as somehow invalid, a 400 status code is an appropriate answer.

所以是的,它是使用HTTP状态code一个很好的主意。德加,状态code是那么容易理解,因为他们没有从应用程序到另一个改变(网络服务)

so yes, it is a really good idea to use http status code. de plus, status code are then easy to understand as they don't change from an application (web services) to another

这篇关于使用HTTP状态codeS反映的Web服务请求成功/失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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