Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http response codes for invalid data and data conflict [duplicate]

Possible Duplicate:
REST response code for invalid data

Have the following REST resource:

POST /user/{primary_key}

The resource is intended to work like an "ADD/UPDATE" operation. This means that it can be used to:

  • Create a new user
  • Update information on an existing user

If a client wanted to create a new user, some information is required:

POST user/{pimary_key}
Paylod:
 - Username - (must be unique)
 - Password

If a client wants to simply update an existing user, the call only needs to include the primary key and the new/changed information. For example:

POST user/{pimary_key}
Paylod:
 - favorite hamburger type

This situation creates the potential for several requests from the client that are invalid:

  • CONFLICT - The client updates an existing user attempting to change the username to a value that is already in use by a different user.
  • MISSING INFORMATION - The client attempts to create a new user without including necessary information such as the username and password.

What are the correct HTTP response codes to return in these cases?

Thanks so much!

like image 230
PepperoniPizza Avatar asked Nov 30 '22 22:11

PepperoniPizza


1 Answers

  1. code 201 for created user, quite obvious
  2. 400 for incorrect input parameters is the most suitable, google API uses it
  3. seems 409 the best for conflicting situation like yours

I would only recommend to separate creation and editing, and use different methods for them - POST to create, PUT to update. What if the user was going to modify something, but had a typo? It is better to show an error

like image 200
allergic Avatar answered Dec 04 '22 12:12

allergic