Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the best/common RESTful url verbs and actions?

Tags:

rest

I'm trying to find some info on the best and most common RESTful url actions.

for example, what url do you use for displaying the details of an item, for editing the item, updating, etc.

/question/show/<whatever> /question/edit/<whatever> /question/update/<whatever> (this is the post back url) /question/list   (lists the questions) 

hmm. thanks to anyone helping out :)

like image 347
Pure.Krome Avatar asked Nov 02 '08 01:11

Pure.Krome


People also ask

What are RESTful URLs?

Although URLs containing parameters within the query string do themselves conform to REST constraints, the term “REST-style URL” is often used to signify a URL that contains its parameters within the URL file path, rather than the query string.

Which of these are the 4 correct types of REST requests?

The most common are: GET, POST, PUT, and DELETE, but there are several others. There is no limit to the number of methods that can be defined and this allows for future methods to be specified without breaking existing infrastructure. The concept of idempotence is relevant to this discussion.

What makes a URL RESTful?

Creating a RESTful Web Service. A RESTful web service request contains: An Endpoint URL. An application implementing a RESTful API will define one or more URL endpoints with a domain, port, path, and/or querystring — for example, https://mydomain/user/123?format=json .


2 Answers

Use URLs to specify your objects, not your actions:

Note what you first mentioned is not RESTful:

/questions/show/<whatever> 

Instead, you should use your URLs to specify your objects:

/questions/<question> 

Then you perform one of the below operations on that resource.


GET:

Used to obtain a resource, query a list of resources, and also to query read-only information on a resource.

To obtain a question resource:

GET /questions/<question> HTTP/1.1 Host: whateverblahblah.com 

To list all question resources:

GET /questions HTTP/1.1 Host: whateverblahblah.com 

POST:

Used to create a resource.

Note that the following is an error:

POST /questions/<new_question> HTTP/1.1 Host: whateverblahblah.com 

If the URL is not yet created, you should not be using POST to create it while specifying the name. This should result in a resource not found error because does not exist yet. You should PUT the resource on the server first. You could argue that by creating a new question, you are also updating the /questions resource as it would now return one more question in its list of questions.

You should do something like this to create a resource using POST:

POST /questions HTTP/1.1 Host: whateverblahblah.com 

Note that in this case the resource name is not specified, the new objects URL path would be returned to you.

DELETE:

Used to delete the resource.

DELETE /questions/<question> HTTP/1.1 Host: whateverblahblah.com 

PUT:

Used to create a resource, or overwrite it, while you specify the resources URL.

For a new resource:

PUT /questions/<new_question> HTTP/1.1 Host: whateverblahblah.com 

To overwrite an existing resource:

PUT /questions/<existing_question> HTTP/1.1 Host: whateverblahblah.com 

...Yes, they are the same. PUT is often described as the 'edit' method, as by replacing the entire resource with a slightly altered version, you have edited what clients will GET when they next do.


Using REST in HTML forms:

The HTML5 spec defines GET and POST for the form element.

The method content attribute is an enumerated attribute with the following keywords and states:

  • The keyword GET, mapping to the state GET, indicating the HTTP GET method.
  • The keyword POST, mapping to the state POST, indicating the HTTP POST method.

Technically, the HTTP specification does not limit you to only those methods. You are technically free to add any methods you want, in practice though, this is not a good idea. The idea is that everyone knows that you use GET to read the data, so it will confuse matters if you decide to instead use READ. That said...

PATCH:

This is a method that was defined in a formal RFC. It is designed to used for when you wish to send just a partial modification to a resource, it would be used much like PUT:

PATCH /questions/<new_question> HTTP/1.1 Host: whateverblahblah.com 

The difference is PUT has to send the entire resource, no matter how big it is compared to what's actually changed, whilst PATCH you can send just the changes.

like image 121
Brian R. Bondy Avatar answered Sep 18 '22 18:09

Brian R. Bondy


Assuming /questions/10 is a valid question then the method is used to interact with it.

POST to add to it

PUT to create or replace it

GET to view/query it

and DELETE to well.. delete it.

The url doesn't change.

like image 40
Allain Lalonde Avatar answered Sep 17 '22 18:09

Allain Lalonde