Why do I need the id parameter in the URI when updating a resource?

I am creating an API and I wonder why it usually has an id parameter in the URI for PUT?

For example, PUT /cars/5 why not PUT /cars? The request object contains an identifier field, is this not enough? Can I get an identifier from this object, or is it some of the disadvantages of this, and is it considered bad?

+5
source share
4 answers

Because if you have to send a request PUTto /cars, semantically, it would mean that you are trying to change the attributes of a set of cars, rather than changing the attributes of a single car. The URI in the RESTful API must indicate the exact resource whose action is valid, so if you change the resource, your URI must specify this resource exactly.

Also from RFC 2616 :

The URI in the PUT request identifies the object enclosed with the request - the user agent knows what the URI is, and the server SHOULD NOT try to apply the request to another resource.

Thus, the specification indicates that if the client knows the unique identifier of the resource, it should be included in the URI.

+4
source

rest "".

, URL- - , / URL- .

wikipedia:

, , URI - REST. , .

+2

PUT .

/cars, .

, , String (URI).

, car id... .

+2

API. API. , , id . , API , , PUT/cars/5, .

, 8 API. GET, POST, PUT, DELETE HEAD. ( , 9 10, ).

, , GET. GET/cars , GET/cars/5 5.So, GET. POST, PUT DELETE. 4 * 2 = 8 ?

Now there are people who will say that PUT / cars will be ambiguous, however, you completely act without an additional identifier field, because, as you mentioned, you already go to the ID field in the request,

The Apigee guys have been studying API projects for some time now. I recommend watching some of their videos in order to better understand what the API is and why some arguments are valid and others not.

Best examples of Apigee

+2
source

All Articles