_rest Design of Web service design based on rest architecture

Source: Internet
Author: User
Tags soap representational state transfer servervariables

Architecture Design for rest

REST (representational state Transfer) is a lightweight Web service architecture style that is significantly simpler to implement and manipulate than soap and XML-RPC, and can be implemented entirely through the HTTP protocol, Cache caching can also be used to improve responsiveness, performance, efficiency, and ease of use over the SOAP protocol.

The rest architecture follows the CRUD principle, and the crud principle requires only four behaviors for resources: Create, read (read), update (update), and delete (delete) to complete its operation and processing. These four operations are atomic operations, an operation that can no longer be divided, by which you construct complex operations, just as mathematically arithmetic are the most basic operations of numbers.

The rest architecture gives people a real understanding of what our network protocol HTTP would look, and the operations of resources including acquiring, creating, modifying, and deleting resources correspond exactly to the get, POST, put, and Delete methods provided by the HTTP protocol, because This rest limits the operation of HTTP to a URL resource within the four of get, POST, put, and delete. This kind of design and development method for network application can reduce the complexity of development and improve the scalability of the system.

Rest Design Guidelines

The rest architecture is designed for Web applications to reduce the complexity of development and increase the scalability of the system. Rest presents the following design guidelines:

All things on the web are abstracted as resources (resource);

Each resource corresponds to a unique resource identifier (resource identifier);

The resources are operated through a universal Connector interface (Generic connector interface);

The various operations on the resource do not change the resource identifier;

All operations are stateless (stateless).

Using the rest architecture

For developers, the concern is how to use the rest architecture, and here's a quick way to talk about the problem. Rest is not just a new architecture, it brings a whole new way of thinking in the Web development process: Designing the system structure through URLs. Rest is a simple set of design principles, an architectural style (or pattern), not a specific standard or architecture. Rest has many successful use cases, the famous delicious and Flickr provide RESTful API usage, client invocation is also extremely convenient, here is a simple rest example I wrote with ASP, from which I can see how simple rest is easy to use.

Client code:

Private Function httpget (URL, method, data) Dim xmlhttp Set xmlhttp = Server.CreateObject ("MSXML2. ServerXMLHTTP ") Xmlhttp.open method, url +"? "+ data, False xmlhttp.setrequestheader" Content-type "," Application /x-www-form-urlencoded; Charset=utf-8 "Xmlhttp.setrequestheader" Content-length ", Len (data) xmlhttp.send (Null) If (XMLHTTP. Status = Then HttpGet = xmlhttp.responsetext Set xmlhttp = Nothing End Function Private function httppost (URL, m Ethod, data) Dim xmlhttp Set xmlhttp = Server.CreateObject ("MSXML2. ServerXMLHTTP ") Xmlhttp.open method, URL, False xmlhttp.setrequestheader" Content-type "," application/x-www-form-u rlencoded; Charset=utf-8 "Xmlhttp.setrequestheader" Content-length ", Len (data) xmlhttp.send (data) If (XMLHTTP. Status = Then HttpPost = xmlhttp.responsetext Set xmlhttp = Nothing End Function Private function httpput (URL, m Ethod, data) Dim xmlhttp Set xmlhttp = Server.CreateObject ("MsxmL2. ServerXMLHTTP ") Xmlhttp.open method, URL, False xmlhttp.setrequestheader" Content-type "," application/x-www-form-u rlencoded; Charset=utf-8 "Xmlhttp.setrequestheader" Content-length ", Len (data) xmlhttp.send (data) If XMLHTTP. Status >= and XMLHTTP. Status <= 599 Then Response.Write "Error occurred:" & XMLHTTP. Status & "-" & Xmlhttp.statustext Else Response.Write Xmlhttp.responsetext End If if (xmlhtt P.status =) Then httpput = xmlhttp.responsetext Set xmlhttp = Nothing End Function Private Function httpdelete (Ur L, method, data) Dim xmlhttp Set xmlhttp = Server.CreateObject ("MSXML2. ServerXMLHTTP ") Xmlhttp.open method, url +"? "+ data, False xmlhttp.setrequestheader" Content-type "," Application /x-www-form-urlencoded; Charset=utf-8 "Xmlhttp.setrequestheader" Content-length ", Len (data) xmlhttp.send (Null) If xmlhttp. Status >= and XMLHTTP. Status <= 599 Then RespoNse.write "Error occurred:" & XMLHTTP. Status & "-" & Xmlhttp.statustext Else Response.Write Xmlhttp.responsetext End If if (xmlhtt P.status =/) Then Httpdelete = xmlhttp.responsetext Set xmlhttp = Nothing End Function Response.Write HttpPost ("HT Tp://localhost/rest/service.asp "," POST "," Do=post ") Response.Write HttpGet (" http://localhost/rest/service.asp "," Get "," Do=get ") Response.Write httpput (" http://localhost/rest/service.asp "," put "," Do=put ") Response.Write
 Httpdelete ("http://localhost/rest/service.asp", "DELETE", "Do=delete")


Service-Side code:

Response.Write Request.ServerVariables ("Request_method")
If (Request.ServerVariables ("request_method") = "Get" Then
 Response.Write "do get" + Request (' do ')
ElseIf (Request.ServerVariables ("request_method") = "POST") Then
 Response.Write "Do POST" + Request (' do ')
ElseIf (Request.ServerVariables ("request_method") = "put") Then
 Response.Write ' do put ' + Request (' do ')
ElseIf (Request.ServerVariables ("request_method") = "DELETE") Then
 Response.Write ' do DELETE ' + Request (' do ') end
if

Note that the IIS server defaults to the put and delete operations of ASP files and returns a "403-forbidden" error by default, so you need to modify the IIS settings. The modification method is: management according to-IIS Information Server-site-Properties-Home Directory-application configuration-configuration-mapping, select ASP-edit-Modify to all actions.

The above is excerpted from the Internet, although it is. NET, but it feels like no matter what language it is, this article helps us to better understand and learn about rest architecture and rest-based Web service, and to share it with you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.