"HTTP Afternoon tea"-http request

Source: Internet
Author: User
Tags curl http request
HTTP Request for browser

It is easy to initiate an HTTP request. For example, you say you want to use a browser to access Reddit. All you need to do is start the browser and enter http://www.reddit.com in the Address bar, and you can see:

The server that hosts the Reddit Web site processes your request and returns a response to your browser. Your browser is smart enough to handle this response, and then show you all the colors, pictures, text, and styles that you've shown above. initiating an HTTP request with the HTTP tool

Because the browser shows us the processed server response, we don't see the response that the server sent us. How do you see the raw HTTP response data?

To do this, we can use an HTTP tool, just like when we enter a URL in a browser, we can send a request to http://www.reddit.com using the HTTP tool. Our HTTP tool, Paw, does not process the response data, so that we can see the original response data, which is about the same length:

What you see in the browser is a huge difference. If you've never seen the original HTTP response data before, this may be a bit of a surprise to you. What you see now is actually the data that your browser receives, but it is the browser that has processed the heap into a human-friendly format.

If you are learning HTTP to be a Web developer, you need to learn to read the original HTTP response data. Of course you don't have to parse this data into a high-definition picture in your mind, but you should have a holistic view of what these responses represent. When you have enough experience, you can go deep into the original HTTP response data to do some debugging work and explore it. using the Moderator (Inspector)

All modern browsers have methods for viewing HTTP requests and responses, often called censors. We use the Chrome viewer to show you how to use it to analyze your browser's HTTP traffic. Open the browser by opening Chrome, clicking the menu in the upper-right corner, selecting Tools, and then selecting Developer Tools. Enter http://www.reddit.com in the address bar to resend a new request to Reddit. Above two steps to make sure the moderator is on, then click the Network tab: The first thing you should be aware of is that there are many items here. Each item is a separate request, that is, simply by accessing the URL, your browser initiates multiple requests, and a request corresponds to a resource (picture, file, and so on). Click on the first request to the homepage, that is www.reddit.com: here, you can see the specific request header, the cookie, and the original response data: In the default sub-tab Headers The header of the request to the server is displayed and the response header is returned by the received server. Next click on the Response Sub-tab to see the original response data. This response data should look similar to the one we used earlier with the HTTP tool.

Another thing to note is that when we use the moderator, there is a bunch of other requests coming back under the Network tab, in addition to the first request:

Why these additional responses occur, who initiated these requests. The thing is, when we ask for resources, the initial request for www.reddit.com returns some HTML. The HTML also refers to other resources such as images, CSS files, JavaScript files, and so on. Your browser, very smart and very strong. It understands that in order to show a Web page that can be viewed, it has to take the resources that are quoted as well. As a result, the browser initiates a request for one by one of each resource in the resulting initial response. When you scroll down the page in the Network tab, you can see all the relevant resources. These other requests guarantee that this web page and other things can be displayed properly on your screen. Overall, the browser's moderator gives you a good experience with reference resources. On the other hand, a purely HTTP tool will only return a huge response to the data, The referenced resource is not automatically pulled. A request that is initiated with curl can be written like this:

$ curl-x GET "http://www.reddit.com/"-M 30-v

Then you see that it should be just a request, a response that contains HTML, but not the extra requests that you see in the browser that are automatically initiated. Request Method

Let's review the figure in the 3rd step above and look at the response data in the Network tab. You will find that there are so two columns of Method and Status.

In this section, let's look at what the information in these columns means.

The data in the method column is often called the HTTP request method. You can think of this Method as a verb to tell the server what you want to do with a resource. The most common two HTTP request methods are GET and post. Use get, the most common HTTP request method, when you want to get information. In the diagram above you should be able to see that all requests for get are used to get the resources that need to be displayed on the page.

Status This column shows the response state code for each request. In the later chapters of this book we will discuss the response in detail. What you need to understand now is that every request gets a response, even if the response is a false response-it's also a response. (This is not exactly true because some of the responses will time out, but at this stage we don't have to worry about these details.) ) GET Request

GET requests typically appear in hyperlinks or in the address bar of the browser. When you enter an address like http://www.reddit.com in your browser's address bar, you are initiating a GET request. You ask the browser to fetch the resources on this address, which means that we have been using get requests throughout the book. The same thing happens when you click a hyperlink on a web app. The default behavior of hyperlinks is to send a GET request to a URL. Let's use the HTTP tool to launch a simple GET request to http://www.reddit.com. To make sure you select the GET item, then enter the URL:

In the right window you can see the original HTTP response data and some other information sent back by the server.

With Curl Reader, you can use the following command in the terminal:

Curl-x GET "http://www.reddit.com/"-M 30-v

We can also use the HTTP tool to send a request with a query string. For example, we started a request with a query string to search all the things in https://itunes.apple.com/that were related to Michael Jackson. The URL used for the request is long:

Https://itunes.apple.com/search?term=Michael%20Jackson

Or that sentence, with Paw, make sure to choose get before initiating the request.

Here we simply send a httpget request with a parameter Term=michael%20jackson to the https://itunes.apple.com/server, where%20 is the URL-encoded character of the space.

The curl command used in this example is this:

$ curl-x GET "Https://itunes.apple.com/search?term=Michael%20Jackson"-M 30-v

These are all the knowledge you need to know at this stage to initiate an HTTP GET request. The main concepts are as follows: Get requests are often used to obtain a resource, and most hyperlinks are get requests. The response to a GET request can be anything, but if the response is an HTML and it references other resources, your browser will automatically initiate requests for those resources, and a purely HTTP tool will not. There are some limitations on size and security when using GET requests. POST Request

We've seen how to use get to get and request data from a server, but what if we need to submit some data to the server? This will refer to another very necessary and important HTTP request method post. When you want to take some action on the server, or send data to the server, you need to use the POST. Let's use the HTTP tool to give an example:

As usual, give the curl command:

$ curl-x POST "http://echo.httpkit.com"-M 30-v

The above picture shows the POST request to http://echo.httpkit.com and the response from the server. The typical post use case in a browser is when you submit a form. The POST request allows us to send larger or more sensitive data to the server, such as images or videos. For example, we want to send our username and password to the server for verification. We can use get to send the data to the server by appending the query string. The flaw with this is obvious: our verification information is visible on the URL. This is certainly not what we want. Using a POST request on a form submission can solve this problem. And the POST request also avoids the query string length limitation when you use GET requests. With POST requests, we can send larger data to the server.

Let's look at another example of submitting a Web Form using a POST request. In the browser our sample table Danchang this:

After submitting the form, you will be redirected to such a page:

Now let's switch to our HTTP tool to simulate what we just did in the browser. We're going to launch a POST request to http://al-blackjack.herokuapp.com/new_player instead of filling out the form in the browser. Here's how we fill out the first form (that's the one we wrote the name on):

or with Curl:

$ curl-x POST "Http://al-blackjack.herokuapp.com/new_player"-D "Player_name=albert"-M 30-v

Note that in the picture and Curl commands we provide additional parameters: Player_name=albert. This is the same as what we did in the "What ' Your Name" input box in the first form.

We can use the moderator to view the content (right-click to select the review element). As you can see, the parameter Player_name we send in the POST request is the content in the name attribute of the INPUT element in this form:

The secret of this is how we send data to the server without a URL, simply by submitting a form. The answer is the text of the HTTP (body). The text contains the HTTP message being transmitted, which is optional. In other words, you can send an HTTP message without a body. When you want to use the text, you can include HTML, pictures, audio, and so on. You can think of the text as a letter wrapped in an envelope (the URL is like an address written on an envelope, visible, and the contents of the letter are invisible in the envelope).

The HTTP tool and the POST request that Curl initiates are the same as when you fill out the form in the browser and then submit it, and then we are redirected to the next page. Take a closer look at the image of the HTTP tool, look at the raw response data inside, and redirect us to the next page of key information in this line of Location:http://al-blackjack.herokuapp.com/bet. The location and its corresponding data are part of the so-called HTTP response header (yes, you might also think that the request has a head, but in this case, this is a response header). Don't be too concerned with these details, we'll discuss the head in the later chapters. When your browser sees the response header, it automatically launches a completely new, entirely separate request to the URL in the location header. The "Make a bet" form that you see is the response to this second request.

If you're confused about the above paragraphs, look two more times. When using a browser, it is important that the browser hides a large amount of HTTP request/response details from you. Your browser initiates a first POST request, gets a response that contains the location header, and then initiates another request without your involvement, and then displays the response to you from the second request. Again, if you are using a purely HTTP tool, you can see the location response header of the first POST request, but the tool does not automatically initiate a second request. (Some HTTP tools have this feature, you can see "Automatically follow redirects" option) HTTP header

The HTTP header allows the client and the server to send additional information in the request/response HTTP cycle. The head, usually a colon-separated key-value pair, is generally in plain text format. We can use the moderator to look at these heads. In the following diagram you can see that both the request and the response have a header:

The figure above shows the various headers passed in a request/response cycle. Further, under Request Headers, we can see that the requests and responses contain different headers:

Request Header

The request header provides more information about the server and the resources to get. Some useful request headers are:

field name description example
Host Server domain name Host:www.reddit.com
accept-language Acceptable language accept-language:en-us,en;q=0.8
user-agent A string that identifies the client

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.