简体   繁体   中英

HTTP What is the purpose of GET method in forms

As far as I understand, the GET method asks the server to send something to the client's browser. I setup a HTTPListener in c# and when I access http://localhost:1330/form.html the request I get from the client is: GET /form.html which means that the client is saying "Hey server, I need the HTML code to display that page in the browser", which makes sense. If I set in form.html a <form> with method=POST , the input fields values are located in the request body which is in context.Request.InputStream in C# which looks similar to this: input_name1=value&input_name2=value2&input_name3=value3... and the urL remains /form.html . This also makes sense, the client days: "Hey server, take this data that was written in the HTML <input> elements" and the server use it, maybe storing it in a database or computing something and send it back to the client. Now If I set the form method to GET, the URL is modified to: /form.html?input_name1=value&input_name2=value2&input_name3=value3 and the context.Request.InputStream remains blank which is the opposite of the POST, in which the InputStream contained the data and the URL had no queries. For me the GET method in forms makes no sense. Why do we need to get the data from the form client side, send it to the server and then getting it back to client unmodified. Why do I send from the browser the data to C# and then sending it back to browser, if I can just get it client side using simple javascript? In the moment the browser makes the GET request with the queries to the server, the client browser already has that data, so why it asks the server to give it if it is already at the client's browser?

Generally speaking, an HTTP GET method is used to receive (unmodified) data from the server. While an HTTP POST is generally used to modify data or add data to the resource.

Think about a search form, for example. There may be a few fields on the form used to filter the results, such as SearchTerm , Start/EndDate , IsActive , etc, etc. You're requesting the results from the server, but not modifying any of the data. Those fields will be added to the GET request by the client so the server can filter and return the results you requested.

GET requests do not have a request body, so the parameters are typically added to the URL (this is defined in the HTTP spec , if you're interested).

An HTTP POST method uses the request body to add the parameters. Typically in a POST you will be adding a resource, or modifying an existing resource.

There are plenty of resources online to learn about the HTTP protocol and HTTP verbs/methods. The MDN article HTTP request methods lists the types of HTTP verb, and A Beginner's Guide to HTTP and REST provides more details, specifically regarding the REST methodology.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM