Tom
Tom

Reputation: 4097

REST URI design: how update

I have a design question of REST URI.

Suppose i have a Car resource (that is mapped in the database with some properties). If i want update the Car resource with the information of another Car i know that i can call

PUT /base_url/api/cars/1

I update the car with id == 1 with the informations in the request body

But if i want update the car with id == 1 with the information of the car with id == 2? (i'd like to pass only id because the copy is handled internally by the server) How can i design this type of request in rest?

Upvotes: 2

Views: 261

Answers (2)

inf3rno
inf3rno

Reputation: 26139

You should use PATCH instead.

The difference between the PUT and PATCH requests is reflected in the way the server processes the enclosed entity to modify the resource identified by the Request-URI. In a PUT request, the enclosed entity is considered to be a modified version of the resource stored on the origin server, and the client is requesting that the stored version be replaced. With PATCH, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version. The PATCH method affects the resource identified by the Request-URI, and it also MAY have side effects on other resources; i.e., new resources may be created, or existing ones modified, by the application of a PATCH.

Something like this would be okay:

PATCH /base_url/api/cars/1 
{template: {id: 2}}

Don't send the id in the query, because it is part of the URI, which is the identifier of your target resource.

Upvotes: 2

zsilbi
zsilbi

Reputation: 131

I would make a PUT request to this URI: PUT /base_url/api/cars/1?clone=2, and pass information (in JSON body).

In the backend I would find the data for car #2, and merge it with the data I got from the request.

This way you could clone the resource, and modify its attributes at the same time.

Upvotes: 1

Related Questions