简体   繁体   中英

REST API - How to design rest api?

I'm little bit confused about when to create a new entity in rest. I have this rest api implemented in node:

GET api/v1/services - get all services

GET api/v1/services/{serviceId}/suppliers - get all suppliers for service id

Now, I want to add another api for getting all suppliers, no matter which service.

Does the following approach is good practice?

GET api/v1/services/suppliers - get all suppliers

PUT api/v1/services/suppliers/{supplierId} - edit by supplier id

Or should we need to create a new suppliers entity?

I hope that @wizard already found an answer for his question. But here are my thoughts.

As per my understanding, the resource "api/v1/services/suppliers" is not a right way to get back all the suppliers. We can use the REST Subresources to represent the relationships so that it will be more readable one. But here Supplier resource can't be used outside of parent resource(Services).

But in this case, we want to get all supplier details and also update the specific supplier information. Hence we require a flexible API. So we have to create another endpoint(/api/v1/suppliers) for working with supplier details.

(/api/v1/suppliers - fetch all the supplier details(GET) and /api/v1/suppliers/{supplierId} [PUT] for updating a specific supplier.

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