Don't translate business errors into HTTP status codes. Status codes are meant to be read by the HTTP client, not by your domain nor by the user itself. We'll use the response body to communicate with these two. If we have to send a message, we send it in the response body.
Creating the follow-up 1
POST /domain/uid/follow HTTP/1.1
...
HTTP/1.1 201 Created
Date: Fri, 20 Dec 2017 14:30:00 GMT
Content-Type: application/json
Location: /domain/uid/follow/123456
Duplicating the follow-up 2
POST /domain/uid/follow HTTP/1.1
...
HTTP/1.1 409 Conflict
Date: Fri, 20 Dec 2017 14:30:00 GMT
Content-Type: application/json
Content-Length: 100
{"message":"already following Shaharyar!"}
We might use custom response headers too.
MyApplication-error: AlreadyFollowingUserError
Unfollowing
2 - Same for when the user is not following someone and tries to
unfollow him.
This second case is slightly different.
If we are trying to remove a resource that doesn't exist
DELETE /domain/uid/follow HTTP/1.1
...
HTTP/1.1 404 Not Found
Date: Fri, 20 Dec 2017 14:30:00 GMT
Content-Type: application/json
Content-Length: 1000
MyApplication-error: ResourceNotFound
{"message":"resource not found"}
Or as @K. Alan Bates has commented (thanks Alan), we could make the operation idempotent, as soon as the URI we request is still reachable.
DELETE /domain/uid/follow HTTP/1.1
...
HTTP/1.1 204 No content
Date: Fri, 20 Dec 2017 14:30:00 GMT
Content-Type: application/json
Content-Length: 100
I have choosen 204 for simplicity. It could be 200 or 202.
In any case, we don't respond with 401 Unauthorized
unless our security service says otherwise.
1:
Edit - Ideally after a new resource creation, we should respond with the new resource location (uri) and the status code 201
2:
Edit - The status code here will depend on the method. If we POST we are requesting a new resource. If it already exists, 409 Conflict is ok. If the request creates a new resource, 201. If we send a PUT, either 200 or 204 are ok. However, idempotency could simplify all of this
POST
request to/domain/uid/unfollow
to unfollow a user?DELETE
request actually, not a POST request