How should a REST API handle PUT when missing parameters?

  softwareengineering

I have a list of users that are being assigned to a certain office. I use checkboxes to select each user and when the client is done, a PUT is performed:

PUT offices/:id

users[0] : 14
users[1] : 12
users[2] : 25

The problem occurs when the server has users saved, but the client decides to clear the board. So he deselects the users and hits save. The problem in this case is that the Ajax request is not sending an empty users[0] : parameter, because each checkbox is unchecked.

In the case when a parameter is not included at all, should PUT overwrite that it and set it to null or should the request include the empty parameter somehow?

1

The answer is in the RFC describing the PATCH method:

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.

Hence, PUT requires you to submit the complete representation of the resource, whereas PATCH may take an incomplete version, or simply some commands on how to apply the patch. This depends on your specific API implementation.

So in your case you actually want to use PATCH, and not PUT.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

How should a REST API handle PUT when missing parameters?

I have a list of users that are being assigned to a certain office. I use checkboxes to select each user and when the client is done, a PUT is performed:

PUT offices/:id

users[0] : 14
users[1] : 12
users[2] : 25

The problem occurs when the server has users saved, but the client decides to clear the board. So he deselects the users and hits save. The problem in this case is that the Ajax request is not sending an empty users[0] : parameter, because each checkbox is unchecked.

In the case when a parameter is not included at all, should PUT overwrite that it and set it to null or should the request include the empty parameter somehow?

1

The answer is in the RFC describing the PATCH method:

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.

Hence, PUT requires you to submit the complete representation of the resource, whereas PATCH may take an incomplete version, or simply some commands on how to apply the patch. This depends on your specific API implementation.

So in your case you actually want to use PATCH, and not PUT.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT