Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including Id in URI for PUT requests

Tags:

rest

I was reading some documents about the appropriate use of URI's using rest services and I came across an example for basic GET .. DELETE requests.

The example uri's were:

Get all users

GET http://mydomain.org/api/users 

Get specific user

GET http://mydomain.org/api/users/1 

Update user

PUT http://mydomain.org/api/users/1 

DELETE user

DELETE http://mydomain.org/api/users/1 

A user resource would be either JSON or XML in the form of:

{     Id: 1,     FirstName: 'John',     LastName: 'Doe' } 

My question is this. To maintain REST principles, is it required to include the id of the resource within the URI for PUT requests?

like image 312
Joshua Dale Avatar asked Feb 13 '12 17:02

Joshua Dale


People also ask

Should Put request have ID in URL?

The target URI of the PUT request should match the target URI of the GET request used to retrieve a representation of the same resource. Therefore, including an "id" in the URI for a PUT request follows precisely the same rules as including an "id" in the URI for a GET request.

Should HTTP verbs be used in URI?

HTTP verbs are preferred if possible, as they are part of the HTTP protocol and as such a standard. It also allows you to use existing security and caching layers on a standard web server, without having to write any bespoke middleware.

Should URI be constructed by the client?

In RESTful HTTP the client should never construct URIs. The service should be well-connected, which means that the client should only ever follow URIs given by the server and make requests to those URIs.

Is URI and API is same?

A URI is used to identify a resource on the web (and other places). A RESTful API uses URIs and HTTP GET/POST/PUT/DELETE to perform CRUD (create, read, update, delete) operations on a web service. e.g.


1 Answers

The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload.

You want to PUT a resource to the same URI you intend to GET it from.

RFC 72314.3.4 PUT

like image 176
abraham Avatar answered Sep 24 '22 05:09

abraham