Design of Web service based on rest architecture

Source: Internet
Author: User
Tags 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 operate than soap and XML-RPC, and can be implemented entirely via the HTTP protocol. Cache caches can also be used to improve responsiveness, better performance, efficiency, and ease of use than the SOAP protocol.

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

The rest architecture gives people a real understanding of the HTTP features of our network protocols, and the operation of resources, including acquiring, creating, modifying, and deleting resources, corresponds exactly to the get, POST, put, and Delete methods provided by the HTTP protocol, as This rest restricts the operation of HTTP to a URL resource within the four of get, POST, put, and delete. This approach to design and development of Web-based applications can reduce the complexity of development and improve the scalability of the system.

  Design Guidelines for rest

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

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

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

Operation of the resource via a common connector interface (generic connector interface);

Various operations on resources do not alter resource identifiers;

All operations are stateless (stateless).

  Using the rest architecture

For developers, the concern is how to use the rest architecture, and here's a brief discussion. Rest is not just a new architecture, it's a 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 a lot of successful use cases, the famous delicious and Flickr provide rest-style API usage, client invocation is extremely convenient, here is a very simple rest example I write with ASP, from which I can see how easy it is to use the rest.

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 = nothingend functionprivate Function httppost (URL, metho d, data) Dim xmlhttp Set xmlhttp = Server.CreateObject ("MSXML2. ServerXMLHTTP ") Xmlhttp.open method, URL, False xmlhttp.setrequestheader" Content-type "," Application/x-www-form-url encoded; Charset=utf-8 "Xmlhttp.setrequestheader" Content-length ", Len (data) xmlhttp.send (data) If (XMLHTTP. Status = $) Then HttpPost = xmlhttp.responsetext Set xmlhttp = nothingend functionprivate Function httpput (URL, metho d, data) Dim xmlhttp Set xmlhttp = Server.CreateObject ("MSXML2. ServerXMLHTTP ") XMLhttp.open method, URL, False xmlhttp.setrequestheader "Content-type", "application/x-www-form-urlencoded; 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 (XMLHTTP. Status = $) Then Httpput = xmlhttp.responsetext Set xmlhttp = nothingend functionprivate Function httpdelete (URL, met Hod, 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 (XMLHTTP. Status = $) Then Httpdelete = xmlhttp.responsetext Set xmlhttp = nothingend functionresponse.write httppost ("http://l Ocalhost/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 ("does") ElseIf (Request.ServerVariables ("request_method") = "POST") Then Response.Write " Do POST "+ Request" ("ElseIf") (Request.ServerVariables ("request_method") = "put") and then Response.Write "Do PUT" + request ( ' Do ') ElseIf (Request.ServerVariables ("request_method") = "DELETE") then Response.Write ' Do DELETE ' + Request ("do") End if

It is important to note that the IIS server defaults to the put and delete operations that do not support ASP files, and "403-forbidden" errors are returned by default, so you need to modify IIS settings. The modification method is: management according to-IIS Information Server-site-Properties-Home Directory-application configuration-configuration-mapping, select ASP-edit-Modify for all actions.

The above for I was excerpted from the Internet, though yes. NET, but feel that regardless of the language, this article is conducive to our rest architecture and Rest-based Web service better understanding and learning, deliberately picked to share with you!

Design of Web service based on rest architecture

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.