0

I'm designing RESTful API for small project.

In my service, There are Group and User. When some User want to get in the group, User can apply to Group. In database, Apply row is created with status column value "WAITING", so I use POST for application to group.

Then owner of Group who created Group can either approve or reject.

When User is approved, Apply row's status value will be updated to "APPROVED" and Member row is created. (Member table represents members of some Group). In this case, which http method used for approving application to study? PUT or POST?

In my knowledge, Since Member row is created by approval, POST seems to be suitable. But, since Apply row's value is updated at the same time, PUT might be good enough.

Which http method is appropriate for this case?

1 Answers1

1

You can, and almost always should separate API and database logic. It is never that simple that you need only basic CRUD operations. In your case if the resource is user's application to a group, then you create an application with POST, as you wrote, because this is creating a new resource. In your case it is a simple insert, but in other cases it could mean other operations, including updates or even deletes. But that is database layer, from the API's point of view, this is a POST.

When you approve or deny an application, the resource is again user's application to a group, and you update it, so I would say this is a PUT or PATCH. Behind the scenes on the database level, it means inserting a new row, but from the API's point of view, this is not a new resource, but modifying an existing.

Of course if your resource is user-group membership, creating a new membership is definitely a POST.

It is really up to you to decide, the same problem can have different solutions. There are good REST API guides available, I recommend reading them, search for 'rest api best practices'.

Peter Koltai
  • 8,296
  • 2
  • 10
  • 20
  • Thanks for nice and easy understanding solution,peter! if I used entity event listener (I use JPA) for creating membership row when status is changed(WAITING -> APPROVED via `PUT` api calls) , is it still `PUT` ? Since from api's point of view , this api only use existing resource(application) and creating a new membership row is some kind of ** behind the scenes business logic **, do i correctly understand? – Sung Woo Hwang Jul 31 '21 at 15:33
  • If you create a REST API backend, it doesn't matter from where do you call the endpoints, you can have multiple clients and other applications as consumers, like mobile applications, browser clients or other backends. This is the main point of it. – Peter Koltai Jul 31 '21 at 17:17
  • To understand what is a resource (it is not application) in this terminology, please refer to [this](https://restful-api-design.readthedocs.io/en/latest/resources.html#:~:text=The%20fundamental%20concept%20in%20any,methods%20that%20operate%20on%20it.&text=Collections%20can%20exist%20globally%2C%20at,contained%20inside%20a%20single%20resource.) or [this](https://stackoverflow.com/questions/10799198/what-are-rest-resources) or [this](https://restfulapi.net/). – Peter Koltai Jul 31 '21 at 17:20