【问题标题】:Http response codes for invalid data and data conflict [duplicate]无效数据和数据冲突的 Http 响应代码 [重复]
【发布时间】:2012-12-17 03:15:22
【问题描述】:

可能重复:
REST response code for invalid data

拥有以下 REST 资源:

发布 /user/{primary_key}

该资源旨在像“添加/更新”操作一样工作。这意味着它可以用于:

  • 创建新用户
  • 更新现有用户的信息

如果客户想创建一个新用户,需要一些信息:

POST 用户/{pimary_key} 有效载荷: - 用户名 - (必须是唯一的) - 密码

如果客户端想要简单地更新现有用户,调用只需要包含主键和新的/更改的信息。例如:

POST 用户/{pimary_key} 有效载荷: - 最喜欢的汉堡类型

这种情况可能会导致来自客户端的多个无效请求:

  • CONFLICT - 客户端更新现有用户,试图将 username 更改为其他用户已在使用的值。
  • MISSING INFORMATION - 客户端尝试创建新用户,但未包含用户名和密码等必要信息。

在这些情况下要返回的正确 HTTP 响应代码是什么?

非常感谢!

【问题讨论】:

    标签: http rest http-status-codes


    【解决方案1】:
    1. 创建用户的code 201,很明显
    2. 400 输入参数不正确最合适,google API 使用它
    3. 似乎 409 最适合像你这样的冲突情况

    我只建议将创建和编辑分开,并为它们使用不同的方法 - POST 来创建,PUT 来更新。如果用户要修改某些内容,但有错字怎么办?最好显示错误

    【讨论】:

    • 关于 POSTPUT 推荐的好点。
    【解决方案2】:

    这是一个很好的“典型”表HTTP responses to RESTful operations

    从该表中,以下是 POST 操作的建议:

    200 (OK) - if an existing resource has been updated
    201 (created) - if a new resource is created
    202 (accepted) - accepted for processing but not been completed (Async processing)
    
    301 (Moved Permanently) - the resource URI has been updated
    303 (See Other) - e.g. load balancing
    
    400 (bad request) - indicates a bad request
    404 (not found) - the resource does not exits
    406 (not acceptable) - the server does not support the required representation
    409 (conflict) - general conflict     
    412 (Precondition Failed) e.g. conflict by performing conditional update
    415 (unsupported media type) - received representation is not supported
    
    500 (internal server error) - generic error response
    503 (Service Unavailable) - The server is currently unable to handle the request
    

    【讨论】:

      猜你喜欢
      • 2011-09-01
      • 2017-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-20
      • 2019-04-25
      • 2018-03-12
      相关资源
      最近更新 更多