ASP. Webapi Pre-Knowledge: http and Restfulapi

Source: Internet
Author: User
The basic understanding of the HTTP protocol is the basis for understanding and using RESTful style APIs, and after understanding these basics, use a variety of restful development frameworks to be handy. I started to use the WEBAPI because of the lack of knowledge of these things, I feel that use a variety of uncomfortable, until familiar with these HTTP knowledge, use WEBAPI developed to feel handy, my understanding, RESTful style API is the HTTP protocol good support, An API that implements HTTP full semantic style.

Before I introduce this knowledge, I need to highlight a misconception that many people have: HTTP predicates and the way data is passed. Most people touch and use the HTTP protocol is in the process of writing web site, in general Web applications, we only use GET, post two predicates, other predicates do not apply, in this habit many people have a few strange cognition: the HTTP protocol only applies to web development, HTTP has only two predicates: Get/post,http invokes data passing only in the form of form k-v; In this kind of cognition, the development of RESTAPI in this style is often nondescript, and the use of ASP. NET Webapi will appear to be nondescript and troublesome. The first thing we need to realize is that the Web site's data interaction is just a scene of HTTP usage, and HTTP can pass in various forms of data.

We start with the first line of http: The first line of HTTP contains three messages: predicates, URLs, HTTP protocol versions. Three data is separated using a space.

Predicates: Predicates are a very important element for the RESTful API, and the WEB API uses predicates as the default route, with the most commonly used predicates: Post\delete\put\get, which correspond to the "Add, delete, change, check" of the four predicates Four actions (post and put who is to change the different information there are always different statements, I actually have a little bit confused ... There is a definition that put is idempotent, and post is not, the put is more emphasis on change and post more emphasis on increasing). The most common predicates are those four, and other predicates have different semantics:

Head: Only the corresponding head is returned, not including the body

TRACE: Diagnosing the data transfer process

Options: Requesting the WEB server to inform it of the various features it supports

There are other predicates that can be queried if needed, but are not commonly used.

Where Get,delete does not contain body,put,post can contain body. And if a predicate contains operations other than semantics, such as GET, with Body,post used to delete resources, is also allowed, which is called overloading of predicates, although HTTP can support overloading of predicates, it is not recommended because it does not conform to standard semantics.

Url:url defines a resource, such as Www.example.com/person , which defines the person as a resource, together with the predicate described above, we provide a group of actions for the person:

Get WWW.EXAMPLE/PERSON/1 that gets the information for the user with ID 1

POST www.example/person/ (the body contains a person's description) creates a person resource

PUT WWW.EXAMPLE/PERSON/1 (the body contains a person's description) updates a person resource

Delete WWW.EXAMPLE/PERSON/1 Remove the person resource with ID 1

HTTP version:

At present the main use is HTTP1.0 and HTTP1.1 Agreement, HTTP2.0 agreement is popularizing stage, use not many. The difference between HTTP1.0 and HTTP1.1 is small, and the difference is not very big for restful. Specific differences can be queried by the relevant documents.

This is what the first line of HTTP is, and then there's a \ r \ n To line up, followed by the HTTP Head section, where HTTP head describes the HTTP request and response. I think HTTP head is the most important part of the HTTP protocol, which contains information such as encoding, body length, content negotiation, and so on, and you can also include some custom information. Let me show you a few of the common head in the RESTful API:

User-agent: User agent, what is the client's request, such as IE, Chrome, fiddler, etc.

Host: Domain name (host is generally used for the server's site bindings, and the URL is the same as the domain name, but in some custom DNS usage, may appear in the host and URL inconsistent domain name)

Authorization: Validation information, this field can contain some information for user authentication, but the method is: schema Authorinfo, in the middle space separated, where the schema represents the validation method, Authorinfo represents the authentication information, A common schema, such as Base:authorinfo, uses a username + password and encodes it with Base64. Or use token, similar to the way the session is.

Accept: The data returned in what serialization mode, in MIME notation, for content negotiation of response data, can contain multiple mime, in order of precedence, such as application/json,application/xml,text/html The specific server can return what type of data needs to be determined by the server support situation, there are some standard mime, can be found; sometimes we also need some custom mime, such as Bson, Protocolbuffer, etc., we can customize mime, develop our own implementation on the service side, These extensions have corresponding extensibility points in the ASP. NET Webapi.

Content-type: Uses a MIME representation that represents the serialization of the body of the request being sent, common such as Application/json, and the application/x-www-form-urlencoded most commonly used by web interactions, Represents the serialization of your body part, which appears in the request and in the response.

HTTP head Part I think is the most important part of the HTTP protocol, which can be configured, the use of the place is too much, and there are too many details, the above for me to list the most common parts of my work, the introduction of these materials are all listed enough to complete a book, We are interested in finding relevant information, in the rest API, content negotiation often makes it very confusing for people to start learning to use rest, so remember that the role and the difference of the Accept,content-type two head, accept what kind of data you want to accept, Content-type represents the encoding of the body in the current request. In ASP. Webapi, if there is content-type in the request and there is no accept, the content in the Content-type is used as the response content negotiation by default.

The response section is also divided into the head and body, response head and request head the biggest difference in response to the first line there is an HTTP code,http Code as an API call state display, it is also important that the rest API most commonly used status code is 2xx,4xx,5xx three segments, While 1XX indicates that work continues, 3XX generally represents redirection, and not many are used in rest APIs. In the most commonly used three status segments, 2XX indicates a successful execution, 4XX indicates a client data error (for example, a parameter check does not pass), and 5XX indicates server-side processing errors, such as unhandled exceptions (such as database connection errors), according to which the status code can initially determine the execution state of the API call.

There is a blank line after the header (\ r \ n) Next is the content, which has specific business data, which is represented by different serialization methods, such as Json,xml, or even HTML, depending on the content-type. As you learn the HTTP API, you can assume that Web applications are also an application of HTTP, but interact in a way that typically uses application/x-www-form-urlencoded as a response to requests and text/html. While Restapi can be used in many other ways to interact, support a wider, Web application is just the use of HTTP transmission of an application scenario, RESTAPI and Web pages can be separated. I think Nancy did better than ASP, and Nancy didn't cut Restapi and Web pages apart, and ASP. NET uses MVC and WEBAPI to separate the two; Request a data, I can ask for an accept for application/ JSON returns JSON data while using text/html to return a Web page; Of course, both of these applications are cut or merged to have pros and cons.

I wrote these for the HTTP protocol is too few and too little, we are interested to find the relevant information, I just write the Web API common parts, let's use a picture to show you this knowledge:

Related Article

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.