0

Is it possible to make the same field compulsory for POST and optional for PUT requests for REST API? Or it depends on teqnology used to implement this request?

For example, there is a POST request to create Car objects and this object has a field model_id which is actually chosen from a dictionary of models. And the POST request requires model_id because the car must have model_id defined.

And I want to have a PUT method which changes some field e.g. serial_num, manufacture_date, color etc. even model_id. But I want this PUT method to change model_id as optional parameter, e.g. I just want to change color and it is Ok.

So is this possible in REST? Does some technologies that implement these request could not allow me to make different set of optional/compulsory params for POST ant PUT?

Ivan Gerasimenko
  • 2,381
  • 3
  • 30
  • 46

1 Answers1

1

Well, you can do whatever you want here. Nobody will kill you if you check fields in the request body and return an error if model_id is specified (or not).

Some guys use POST method to update entities in API.

If you want to be closer to standards better use PATCH for partial updates.

You can find a lot of interesting info here:

  1. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
  2. Use of PUT vs PATCH methods in REST API real life scenarios
  3. https://www.rfc-editor.org/rfc/rfc7231#section-4.3.4
  4. https://www.rfc-editor.org/rfc/rfc5789

One important thing from 4. source:

The PUT method is already defined to overwrite a resource with a complete new body, and cannot be reused to do partial changes.

And another one sentence from 3. source:

Generally speaking, all implementation details behind the resource interface are intentionally hidden by the server.

Community
  • 1
  • 1
Peter Gerasimenko
  • 1,906
  • 15
  • 13