I have an old friend. We call him panda. Hairline and will touch the back of the head, big frame gold wire glasses can not cover the dark circles under the eyes, looks particularly “programmer”; The dress is also very “eclectic”, the upper body is a shirt suit, the lower body is jeans with slippers.

My feelings and the panda is very good, after graduation he went to Shanghai and I began to north drift, but every holiday home we will have dinner with friends, the home for Chinese New Year is no exception, we several friends went to the famous small bench sauce bone, zaozhuang after dinner he gave us a talk about 4 years of experience to come to the company’s interview, it is said that with the interviewer has a fascinating game; We listened with relish.

The following is a conversation between Panda and manager Liu, the interviewer.

Interviewer: Xiao Li, what are your common HTTP request methods?

Panda: Well, THE HTTP/1.1 protocol defines eight methods, sometimes called “actions,” to indicate how request-URl-specified resources operate

  • In HTTP1.0, three request methods are defined:GET.POSTHEADMethods.
  • In HTTP1.1, five new request methods have been added:OPTIONS.PUT.DELETE.TRACECONNECTmethods

But the ones we usually use are GET and POST requests.

Interviewer: Well, what are the differences between GET and POST requests? (as expected into the set, see you boy have a few catties a few two)

Panda :(actually this is 36 plan 17 plan to throw a brick to attract jade. I last night specifically read the elder brother’s article, asked the above questions to the interviewer put a hook, as expected the hook. Ok, first give him the official standard answer, see what he says)

Panda: Well… I haven’t paid much attention to it. As far as I know, there are probably several differences:

  1. GET requests pass parameters in the URL with length limits, whereas POST does not.
  2. GET is less secure than POST because parameters are exposed directly to the URL and therefore cannot be used to pass sensitive information. POST data is not displayed in the URL. Is placed in the Request body.
  3. GET accepts only ASCII characters for the data type of the argument, while POST has no restrictions.
  4. GET request parameters are kept intact in browser history; Conversely, the POST request parameters are not retained by the browser.
  5. GET requests can only be URL coded (Application/X-www-form-urlencoded), while POST supports multiple encoding methods.
  6. GET requests are actively cached by browsers, whereas POST is not, unless set manually.
  7. GET is harmless when the browser falls back, while POST resubmits the request.

Interviewer: (did not pay attention to, that you also back a not bad. Why don’t you put it here with me? Let me teach you.

Interviewer: Does a Get Request have a Request body? If so, can arguments be put in like Post requests?

Panda :(ho ho, looks like we have a chance to give him half an hour of what I prepared carefully yesterday [manual head scratching] move over, I’m going to play bo B)

Panda: Actually, GET and POST are essentially the same. They are both HTTP methods for sending requests. HTTP, on the other hand, is a TCP/ IP-based protocol for how data is communicated across the World Wide Web.

World Wide Web: WWW is short for World Wide Web, also known as Web, 3W, etc

The underlying layer of HTTP is TCP/IP. So the bottom layer of GET and POST is also TCP/IP, that is, both GET and POST are TCP links.

GET and POST can do the same thing. Technically, it makes perfect sense for you to add a request body to GET and a URL to POST.

Here’s an example:

TCP is like a car. We use TCP to transport data. It’s very reliable.

But if the roads were filled with cars that looked exactly the same, the world would look like a mess, and the delivery cars would be blocked by the cars in front of them with loads of goods, and the entire transportation system would surely break down.

To avoid this, traffic rules HTTP was born. HTTP provides several service categories for automobile transportation, including GET, POST, PUT, and so on,

HTTP states that when a GET request is executed, the car is labeled AS GET (with method set to GET) and that the transmitted data is placed on the roof of the car (in the URL) for easy recording.

If it is a POST request, put a POST label on the car and put the goods in the car (request body).

Of course, you can use GET to hide goods in the train, but that’s not disgraceful; You can also put some data on the roof of the car when you POST, which also looks silly.

HTTP is just a code of conduct, whereas GET and POST are essentially TCP links, no different. However, due to HTTP regulations and browser/server restrictions, they are applied differently.

Interviewer: (Oh, this guy really understand this area ah, it seems that I misunderstood him, did you meet a big guy?)

Interviewer: Xiao Li, you are right. What about the length limit of the transmission parameter in THE URL you just said in Get and Post?

Panda: In fact, there is another important role in the Web: transportation companies.

Different browsers the Client side (initiating HTTP requests) and the server side (accepting HTTP requests) are different transportation companies.

In theory, though, you can stack an infinite amount of goods on the roof (with an infinite number of parameters in the URL). But shipping companies are not stupid. Loading and unloading costs are high, and they limit the risk of a single shipment. Too much data is a burden on browsers and servers.

The unwritten rule of the industry is that (most) browsers generally limit urls to 2K bytes, while (most) servers handle urls up to 64K.

Any excess will not be processed. If you use the GET service to hide data in the request body, different servers will handle it differently. Some servers will unload the data for you and read it, while others will ignore it.

So while a GET can take a request body, there is no guarantee that it will be received.

Example: I have dealt with a bug before, the user did not respond to the query. After checking the log, my colleague found that several parameters were undefined, which was very strange. Finally, it was found that the first query parameter of the Get request was too long, which led to part of the server behind the URL could not receive it. Just place the parameters after the Request body.

Interviewer: Is the parameter writing method of GET method fixed?

Panda: In the agreement, our parameters are written in? After that, we split it with ampersand. As follows:

http://ip:port/project/getUserInfo? username=chenhhaha&age=26&sex=2
Copy the code

As we know, the process of packet parsing is to obtain TCP data, use tools such as re to obtain Header and Body from the data, and then extract parameters.

For example, the token is added in the header request header to verify whether the user logs in.

In other words, we can specify how to write parameters, as long as the server can interpret them.

Interviewer: So is POST more secure than GET?

Panda: Some people say that POST is safer than GET because the data is not visible on the address bar.

However, from a transport point of view, they are not secure, because HTTP is transmitted over the network in clear text, as long as the network node to capture the packet, the complete data packet.

In fact, the only way to secure transmission is encryption, which is HTTPS.

Interviewer: HMM, good for you, Xiao Li. It seems that you know a lot about HTTP protocol. Do you know the difference between Get and Post requests?

Panda: (It seems that the interviewer is right if I asked to be satisfied? Pity pity. Watch me set him up)

Panda: To tell you the truth, I worked a lot of overtime in my last company. I just signed the resignation contract yesterday. Your company is the first company I met with, so I didn’t make enough preparation.

Interviewer: That’s ok. Just talk about it as you understand it. I’d better think it over. Don’t miss the opportunity and lose a talent who can work overtime for our company.

Panda: Yeah, well, a GET request generates a TCP packet; The POST request generates two TCP packets.

  • GET: The browser sends HTTP headers and data, and the server responds with 200.
  • POST: The browser sends a header, the server responds with 100 continue, the browser sends data, and the server responds with 200 OK (returns data).

For example, GET only needs the car to make one trip to deliver the goods, while POST has to make two trips. In the first trip, he goes to the server to say “Mr. Iron, I will send a batch of goods later, you are ready to receive it”, and then sends the goods back.

Since POST takes two steps, theoretically consuming a little more time, it looks like GET is more efficient than POST. But it wasn’t. It turned out to be a pit. In my opinion:

  1. GET and POST have their own semantics and cannot be used together.
  2. According to research, under the condition of good network environment, the time difference between sending a packet and sending two packets can be ignored basically. In the case of poor network environment, the TCP of two packets has great advantages in verifying the integrity of packets.
  3. Not all browsers send packages twice in POST, Firefox only sends them once. I used Chrome last year to test and found that it only sends once, so I think the poor performance of Get and POST can be ignored artificially.

Learning technology friends so far, feel learned something remember to help a thumbs-up oh, MMMM da.


Interviewer: Xiao Li, what you said is very good. Wait a minute.

Panda: (slips away?? Shouldn’t ah, I this wave operation is not sweet? Is it, he also read the elder brother’s article, was found out? -_ – ‘|)

Five minutes later, the interviewer comes in with a drink…

Interviewer: Xiao Li, how do you like the coffee? (The ancient routines are popular and learned well)

The e panda drank a mouthful, said: Manager Liu, this is authentic cappuccino ah, I usually like to drink, but it is too expensive.

Yi? I didn’t see you had a coffee shop before I came in. Is this?

Xiao Li, I saw in your resume that you like drinking coffee. Fortunately, our company has a special water bar. One of the benefits is a free cup of coffee or drink every day. You can drink it more often in the future.

Interviewer: (Actually, this perk is offered because our company works so many overtime hours. Don’t ask, I won’t say.

Panda: Manager Liu means I’ve had the interview, right? Great!

Panda: By the way, what time do you get off work every day? With the development of a bit? (It’s not because I work overtime that I get free coffee. I’ll have to feel it out.)

Interviewer: (this problem I listened to countless times, see my watertight fuzzy answer —- true · old man)

Interviewer: Ha ha, yes, the water bar always leaves work by the hour. If someone in our company works overtime, you can go to the water bar and drink by yourself. The overtime is free.

Interviewer: Xiao Li, if you have no problem here, I think you can start next Monday.

Panda: (What he said means the company should only work overtime occasionally, which I can’t refuse! Gee, it just doesn’t feel right.

Panda: Ok, Manager Liu. See you next week.

From: chensj.blog.csdn.net/article/det…

Give a [look], is the biggest support for IT elder brotherCopy the code