Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modelling operations in REST service

Tags:

rest

I know that these type of questions have been asked before. I have solution for my problem and I want to know if I am breaking REST or HTTP principals anywhere.

In my system I have a resource called member which supports usual GET/POST/PUT operations. Member has a status of Active and Disabled. I need to model the operation of disabling the user. I understand why following would be a bad idea from REST perspective

POST api/member/john.smith/disable

I have read a solution to accept a resource that represents the request to disable a member, something like below

public class DisableMemberRequest
{
    public string Username {get; set;}
}

And then a POST on above resource

POST api/DisableMemberRequest

While this approach sounds reasonable, I feel this is not right in terms of clean API interfaces. It can be debatable whether the response of the above request should be a 200 OK or 201 Created or 202 Accepted.

I am thinking, I would crate a new resource called DisabledMember and a PUT on this resource would mean that particular member should be disabled as below

PUT api/disabledmember/john.smith

This looks to be a perfectly valid design from REST/HTTP perspective to me. But I am no expert and would like to validate this with people who have been doing this for long time.

EDIT

I am adding these details after interacting with fellow programmers on this page. The process of disabling the member is not only about setting a status flag on the member. There are other workflows that need to be triggered when a member is disabled.

like image 674
Suhas Avatar asked Jun 21 '13 23:06

Suhas


1 Answers

One way that I like to do things like this is to define a resource that represents the set of disabled members. To disable a member, you add that member to the set of disabled members. It could look something like this.

POST /api/DisabledMembers
Content-Type: text/uri-list

http://example.org/api/members/john.smith

If you want to reverse the operation, you could do

POST /api/ActiveMembers
Content-Type: text/uri-list

http://example.org/api/members/john.smith

This approach has the benefit of the fact that doing GET /api/DisabledMembers would be a perfectly natural thing to do. Also, by using text/uri-list it becomes easy to disable/reactivate a set of members all at the same time.

like image 139
Darrel Miller Avatar answered Nov 12 '22 22:11

Darrel Miller