【问题标题】:When should HTTP 400 Bad Request be used as a response code?什么时候应该使用 HTTP 400 Bad Request 作为响应代码?
【发布时间】:2012-02-17 12:21:36
【问题描述】:

根据维基百科,响应码 400 表示The request cannot be fulfilled due to bad syntax.

这是在给出无效但语法正确的数据时返回的适当状态代码吗?如果不是,那是什么?

【问题讨论】:

    标签: httpresponse


    【解决方案1】:

    我不自称是专家,但让我们比较一下the spec 所说的:

    400 Bad Request - The request could not be understood by the server due 
    to malformed syntax....
    

    因此 400 似乎特定于语法错误。这是另一种在语法良好但值不好时看起来更好的方法:

    409 Conflict - The request could not be completed due to a conflict with 
    the current state of the resource...
    

    在决定之前您需要阅读the full description for 409(它会更长),但这似乎是给定场景的更好选择。它说 PUT 请求很可能会发生冲突,但是当我读到它时,它似乎可以在输入变量无效的任何时候应用。这就是我认为我将用于我正在构建的 RESTful API 的内容。

    【讨论】:

      猜你喜欢
      • 2020-04-23
      • 1970-01-01
      • 2013-11-09
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      相关资源
      最近更新 更多