简体   繁体   中英

REST API PATCH without request body

I'm developing an API for events management. I have the basic GET and POST methods but now I have to deal with the event resource edition.

A user can edit all the event information using:

  • PUT /event/:eventId

But also it can cancel the event (not deleting it, but changing it's status property).

I've thinking on using this endpoint:

  • PATCH /event/:eventId and send a body with only the new status property value.

I think this is a good approach but then I have noticed that the status can only be set to CANCELLED , the other allowed status for the event are changed automatically in the business logic in certain cases.

So sending the status field doesn't make sense at all if you only can change it to one possible value.

Therefore, is it possible and not a bad practice to send no body to a PATCH method? Thanks.

I would suggest to make PATCH endpoint to /event/{eventId}/status. There is no need to put payload to your PATCH request, payload is optional.

API should be meaningful to end user. With PATCH you are letting user know that you want to to a partial update on event record for provided eventId and attribute you want to perform action is status.

In addition, make sure your API doc provides the detail that status will be set to CANCELLED by default. And in future, if required you can scale API by adding payload {"status": "CANCELLED | ENABLED | .." }

I would suggest to add payload to body of PATCH /event/:eventId .

If in any case you need to add a new attribute for updating an event's attribute, you will not be able to decide if you need to update the status or not. This can future proof your endpoint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM