RESTful thread?

So...

I read a little about REST , and the idea behind this sounds good, but the question is, can it easily integrate into a standard webpage stream?

For example, a user creates an element, a blog post, or whatever you have, and now he wants to delete it, so he clicks the "delete" link on the page. Now what? How do we issue a DELETE request, say, http://mysite.com/posts/5 ? And how do we handle this request? I have no experience with cURL or anything else, but due to its appearance I would have to curl_init('http://mysite.com/posts/5') and then work a little. But where would I even put this script? It should be on another page that breaks the whole idea of ​​REST. Then I would just be GET another page, which, in turn, would be DELETE the page that I originally planned?

Is this why people rarely use REST or is there really a good way to do this?


Looks like I need to clarify. People suggest including words such as "DELETE" and "POST" in the URL. I believe that REST dictates that we have a unique URL for each resource, but not for every action on this resource. I assume this also means that we have only one and only one URL for each resource. that is, I want to be able to delete or view the contents of a specific record from a single URL (by sending either DELETE, PUT, POST, or GET), and not different URLs with additional parameters

+1
source share
8 answers

With a quiet server, the same url (say / books / 1) can respond to many different verbs. These verbs, GET, POST, PUT and DELETE together with this path indicate what you want to do with the data on the server. The answer will answer your request.

REST is access to data in a predictable and reasonable way.

If you came from a strong PHP background, where each URL should map to a specific file, you're right, that doesn't make sense. The two most notable RESTful development environments, ASP.NET MVC and Rails, have special servers (or server logic) that read verbs and do special routing for you. This is what allows the "normal flow" of the application to go through as you expected. For PHP, there are frameworks that help with this, such as WSO2 WSF .

How REST works with web browsers

Take for example your example. We have messages and we want to delete them.

  • Let's start with a URL, for example / posts / 4. As you might expect, this shows post 4, its attributes, and some actions you could take. A request to provide this URL will look like GET /posts/4 . The response contains HTML that describes the element.

  • The user clicks the "Delete Item 4" link, part of the HTML. This sends a request like DELETE /posts/4 to the server. Note that he reused URL /posts/4 , but the logic should be different.

    From HTML forms and web browsers, many of them will by default change the link with the method = "delete" to the link method = "post". You will need to use Javascript (something like this ) to change the verb. Ruby on Rails uses a hidden input field ( _method ) to indicate which method should be used on the form, as an alternative.

  • On the server side, the delete element logic is executed. He knows how to do this because of the verb in the request ( DELETE ), which corresponds to the action being performed. It is a key point of REST that HTTP verbs become meaningful.

  • After deleting an element, you can respond to a page, for example, "yep, done", or "no, sorry, you can’t do this," but it makes sense for the browser to put you somewhere else. An element deleted with a response to a redirect to GET /posts makes sense.

If you look at the server log, it will be very clear that everything was done with the server, but this is not as important as ...

How REST works with arbitrary data

Another key point of REST is that it works well with multiple data formats. Suppose you are writing a program that would like to read and interact with the blog programmatically. You may need all the messages provided in XML, instead of having to clear HTML for information.

GET /posts/4.xml intuitive: "Server, please give me xml describing message # 4." The answer will be that xml. The RESTful server makes it obvious how to get the right information.

When you made the DELETE /posts/4.xml request, you ask: "Server, please delete item # 4." An answer like “Good, of course” is usually enough to express what happened. Then the program can decide what else it wants and make another request.

+6
source

Depending on the structure used, there are models that determine how actions are processed for each resource.

Basically, using another parameter, you want to send the resource what action to perform. This parameter can be sent, for example, through AJAX / JS.

If you want to do this without javascript / ajax (in case of failure), then the POST method of the form will work, sending the resource an additional ACTION parameter.

Of course, in both cases, you must consider security and make sure that they do not send the resource into action, which they should not be. Be sure to check the backend and send the appropriate response or error message.

Client-side scripting, whether through JS / Ajax or a POST form or other methods, requires extra precaution.

Edited after clarification from the poster.

+1
source

Well, one way is to call AJAX using the DELETE method.

+1
source

I do not think REST is rarely used. You are using it right now in StackOverflow. As for your specific example, you can send DELETE requests, although XMLHttpRequest is in browsers that support it. When JS is turned off or for incompatible browsers, you can do something like:

POST http://foo.com/delete?post=5

Not perfect, but even more relaxed than many sites.

EDIT: changed to POST

+1
source

The Facebook REST server is pseudo-one, you can do it the same way they do by requesting the post method: POST, GET, etc. action and other values ​​that you need for this request.

Why am I saying facebook is a pseudo-REST server ?: okay, one of the REST Principles says

  • Each resource is uniquely addressed using universal syntax for use in hyperlinks.

on facebook you only have /server.php and there you make a request, even for (POST, GET, PUT, DELETE ...)

another way uses mod_rewrite and parses the url that the client requests

EDIT: Just found this one , looks interesting. Have some fun!

+1
source

Do not overdo it. You cannot do this with direct HTML forms and a browser. They do not support the DELETE method . Ajax can do this.

I want to DELETE to VIEW the contents of a specific message from one URL (by sending either DELETE, PUT, POST or GET), and not different URLs with additional parameters

Delete to view? I'm not sure I understand, but your removal should be done through the headers and not through the URL. The delete method should not return a view. REST is a service , not all requests are for visual consumption.

+1
source

If you really have no choice about using the verb DELETE, I would suggest the following:

 POST http://mysite.com/Trashcan?resourceUrl=/Customer/75 

Which url you use does not really matter for REST, however, it’s easier to understand how REST interacts if your URLs completely exclude verbs.

I have seen so many questions from both Rails and ASP.NET MVC users that should go beyond the standard "actions", and therefore it is tempting to just add a new action to the controller. The problem with this is that you simply excluded the unified restriction of the REST interface.

Metaphor metaphor is not the only way to remove deletions, but I would say that it is as clear as reading “delete” in the URL.

Here are a few "nouns" of verb replacement.

 POST http://mysite.com/Printer/75/PrintQueue?url=http://mysite.com/Document/xyz POST http://mysite.com/CurrentLogins?user=bob POST http://mysite.com/QueryProcessor?query=FindMyInformation POST http://mysite.com/SearchEngine?searchTerms=cat,blue,furry POST http://mysite.com/OrderProcessor?cart=http://mysite.com/user/2323/cart 

Sometimes you need to think a little about coming up with a noun-based website and it may seem pedantic to try to do it, but for me the advantage comes from the ability to manage my variables. I am going to have a variable amount of resources in my interface, no matter what I do, if I can fix the number of verbs that can work on these resources, then I will reduce one of my variables.

+1
source

Another way to do this is by assuming that a webbased / webapplication based request will have 2 submitb buttons. Since PUT and DELETE use the same uri / url. You can add a specific delete form and attach a specific name to this delete button, so when it is sent via a message, you can use this button name to turn the action into DELETE

0
source

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


All Articles