What REST PUT/POST/DELETE calls should return by a convention?

前端 未结 5 2036
离开以前
离开以前 2020-12-04 05:54
  1. According to the \"REST ideology\" what should be in the response body for a PUT/POST/DELETE requests?

  2. What about return codes? Is HTTP_OK

5条回答
  •  一向
    一向 (楼主)
    2020-12-04 06:33

    I like Alfonso Tienda responce from HTTP status code for update and delete?

    Here are some Tips:

    DELETE

    • 200 (if you want send some additional data in the Response) or 204 (recommended).

    • 202 Operation deleted has not been committed yet.

    • If there's nothing to delete, use 204 or 404 (DELETE operation is idempotent, delete an already deleted item is operation successful, so you can return 204, but it's true that idempotent doesn't necessarily imply the same response)

    Other errors:

    • 400 Bad Request (Malformed syntax or a bad query is strange but possible).
    • 401 Unauthorized Authentication failure
    • 403 Forbidden: Authorization failure or invalid Application ID.
    • 405 Not Allowed. Sure.
    • 409 Resource Conflict can be possible in complex systems.
    • And 501, 502 in case of errors.

    PUT

    If you're updating an element of a collection

    • 200/204 with the same reasons as DELETE above.
    • 202 if the operation has not been commited yet.

    The referenced element doesn't exists:

    • PUT can be 201 (if you created the element because that is your behaviour)

    • 404 If you don't want to create elements via PUT.

    • 400 Bad Request (Malformed syntax or a bad query more common than in case of DELETE).

    • 401 Unauthorized

    • 403 Forbidden: Authentication failure or invalid Application ID.

    • 405 Not Allowed. Sure.

    • 409 Resource Conflict can be possible in complex systems, as in DELETE.

    • 422 Unprocessable entity It helps to distinguish between a "Bad request" (e.g. malformed XML/JSON) and invalid field values

    • And 501, 502 in case of errors.

提交回复
热议问题