I have an url like this:
http://www.example.com/users/
I want to create a new user with a PUT request:
requests.put('http://www.example.com/users/john/', data={'password': 1234})
Actually this request creates a new user resource and returns a 201 "Created" status code, if i want to update that resource i can make a request like:
requests.put('http://www.example.com/users/john/', data={'username': 'jane'})
Well, i can return a response with a 301 "Moved Permanently" status code and the new created url in the Location header. The problem comes when i am trying to implement such logic in the controller code, in some part of the logic i have some code that checks for the existence of the username, something like:
if username in users:
# do something
If i use PUT for create a new resource, in this case an user, saying that the username already exists i could return a 409 "Conflict" status code with a message like "Username already exists, choose another one", if the username doesn't exists i just return a 201 "Created", logically this creation operation not require authentication cause is like a "sign up" operation.
In the other hand when i request for update the resource i need to do some kind of authentication in order to allow an user to update his account information.
So if i use the username variable to determine what operation i have to perform, and the username name already exists, i can't know when to respond with an authentication required or an invalid username response.
My first thought was to use a POST request to create a new user and PUT just for the update behavior, but it seems that use POST for creating a resource who's identifier is not created by the server is a bad idea.
So my question is if there are any way of implement this both create and update functionality with a PUT request?