1

If I can do the following when creating a record,

URI: /api/roles/
Method: POST
Content: { Id: 0, Name: 'Admin', Description: 'Administration Role' }


Can I just do this if I want to update a record?

URI: /api/roles/
Method: PUT
Content: { Id: 1001, Name: 'Admin', Description: 'Administration Role' }

...because I can just get the identifier in the Id field. Instead of doing this?


URI: /api/roles/1001
Method: PUT
Content: { Id: 1001, Name: 'Admin', Description: 'Administration Role' }

...specifying the Id value twice. By the way, I cannot remove the Id field in the content.

If not, why? What is the main reason or advantage of appending the id of a resource in HTTP PUT, putting the HTTP specification aside?

Dennis Laping
  • 682
  • 1
  • 7
  • 18
  • 1
    generally the `id` is passed to uniquely identify a resource, and the content is used to update it's current state.I believe you are allowed to do what you want. – Amit Kumar Ghosh Aug 18 '15 at 05:21
  • 1
    I think if you put the HTTP principles aside, then there is really no advantage or reason. But I think you lose clarity or ease of use of your API. Without applying the HTTP principles, nothing is stopping you from making your update operations available through a GET or POST request. – Frank Fajardo Aug 18 '15 at 07:22

2 Answers2

1

Adding ID resource denotes one of the steps of RESTful API

You could look for further discussion here : What is the advantage of using REST instead of non-REST HTTP?

RESTful design principles specify HATEOAS which roughly states that interaction with an endpoint should be defined within metadata that comes with the output representation and not based on out-of-band information.

http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

Community
  • 1
  • 1
Muks
  • 134
  • 9
1

I agree there seems to be a strange redundancy in specifying the ID in the URL as well as the request body.

As I see it, the reason is not technical, but 'cultural'.

Whenever the URL is /api/roles/ you indicate that the list is being manipulated, whereas /api/roles/{id} clearly points to the target being a list item.

morsor
  • 1,263
  • 14
  • 29