Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is www.example.com/post/21/edit a RESTful URI? I think I know the answer, but have another question

I'm almost afraid to post this question, there has to be an obvious answer I've overlooked, but here I go:

Context: I am creating a blog for educational purposes (want to learn python and web.py). I've decided that my blog have posts, so I've created a Post class. I've also decided that posts can be created, read, updated, or deleted (so CRUD). So in my Post class, I've created methods that respond to POST, GET, PUT, and DELETE HTTP methods). So far so good.

The current problem I'm having is a conceptual one, I know that sending a PUT HTTP message (with an edited Post) to, e.g., /post/52 should update post with id 52 with the body contents of the HTTP message.

What I do not know is how to conceptually correctly serve the (HTML) edit page.

Will doing it like this: /post/52/edit violate the idea of URI, as 'edit' is not a resource, but an action?

On the other side though, could it be considered a resource since all that URI will respond to is a GET method, that will only return an HTML page?

So my ultimate question is this: How do I serve an HTML page intended for user editing in a RESTful manner?

like image 546
tmadsen Avatar asked Jan 22 '23 03:01

tmadsen


2 Answers

Another RESTful approach is to use the query string for modifiers: /post/52?edit=1

Also, don't get too hung up on the purity of the REST model. If your app doesn't fit neatly into the model, break the rules.

like image 77
Ned Batchelder Avatar answered Feb 08 '23 14:02

Ned Batchelder


There is no such thing as a RESTful URI. It is false concept as URIs should be completely opaque to the client.

If it helps you to properly implement the HTTP uniform interface by avoiding verbs in your URIs then that's great, but don't feel constrained by what your URI looks like. It is very limiting to think of resource modeling as type of data modelling. A RESTful system usually needs to do way more than just CRUD operations, so you need to be creative about what resources you make available in your system.

If you create a URL and dereferencing it returns a 200 status code, then that URL refers to a resource. If you create another URL and it also returns a 200, then that is a difference resource.

That means:

http://example.org/customer/10.xml
http://example.org/customer/10.json
http://example.org/customer/10?format=xml
http://example.org/customer/10?format=json

are 4 different resources, and

http://example.org/customers
http://example.org/customers?closed=true
http://example.org/customers?page=2&pagelength=20

are also different resources.

Therefore to answer your question, if you do

GET /post/52/edit 

and it returns a 200 status code and a representation, then it must be a resource.

like image 32
Darrel Miller Avatar answered Feb 08 '23 16:02

Darrel Miller