Home > Software engineering >  Best API URL naming for a model with 2 belongTo relations
Best API URL naming for a model with 2 belongTo relations

Time:12-15

I have a model named Booking with a User, a Service and the createdAt fields.

The URL to create a new Booking should be:

  • [POST] /bookings?user_id={user_id}&service_id={service_id} or
  • [POST] /users/{user_id}/bookings?service_id={service_id} or
  • [POST] /services/{service_id}/bookings?user_id={user}

CodePudding user response:

All of those are fine, but....

It's a good idea to pay attention to cache invalidation. The target URI of your request is one of three resource identifiers that will be automatically invalidated by a successful POST request. You can take advantage of that to evict a web page in the client's cache whose information will be changed by the message you send.

For example, if creating a new booking adds a new link to the /bookings resource, then

POST /bookings

has the advantage of automatically evicting the previously cached copy of the resource that doesn't include the link you just created.

  • Related