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

I'm almost afraid to pose this question, there should be an obvious answer that I missed, but here I go:

Context: I am creating a blog for educational purposes (I want to learn python and web.py). I decided there were posts on my blog, so I created a Post class. I also decided that messages could be created, read, updated or deleted (therefore CRUD). Therefore, in my Post class, I created methods that respond to the POST, GET, PUT, and DELETE HTTP methods). So far, so good.

The current problem I am facing is conceptual, I know that sending a PUT HTTP message (with a modified message), for example, / post / 52, should update the message with identifier 52 with the contents of the HTTP message body.

What I don't know is how to use the edit page (HTML) correctly.

Will it do this: / post / 52 / edit violates the idea of ​​a URI, since β€œedit” is not a resource, but an action?

On the other hand, can this be considered a resource, since all the URI will respond to is a GET method that will only return an HTML page?

So my last question is this: how can I serve an HTML page for editing a user in RESTful?

+4
source share
4 answers

There is no such thing as a RESTful URI. This is a false concept, as URIs must be completely opaque to the client.

If this helps you implement the uniform HTTP interface correctly by avoiding the verbs in your URIs, then that’s great, but don’t feel limited to what your URI looks like. It is very important to think of resource modeling as a data modeling model. A RESTful system usually needs to do more than just CRUD operations, so you need to be creative about what resources you provide on your system.

If you create a URL and look for it, it returns a status code of 200, then that URL refers to the resource. If you create another URL and it also returns 200, then this is a difference resource.

It 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 

- 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 view, then it should be a resource.

+3
source

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

In addition, one should not depend too much on the purity of the REST model. If your application does not fit neatly in the model, break the rules.

+4
source

Instead of calling it /post/52/edit , what if you called it /post/52/editor ?

Now it is a resource. The dilemma is averted.

+2
source

I do not think that / object / id / action is part of the REST specification.

Is your editor universal for all objects? Then maybe your url should look like

 /editor/object/id 

An action is an HTTP verb (GET, PUT, DELETE, POST) and is assumed to be part of the HTTP request, not part of the URL. For a more complete summary, check out this RESTful_web_services Wikipedia article .

0
source

Source: https://habr.com/ru/post/1308547/


All Articles