What is HTTP?
The Hypertext Transfer Protocol (HTTP) is a protocol that allows clients and servers to communicate with each other.
HTTP is a request-response protocol that communicates between a client and a server.
A client (browser) sends an HTTP request to the server, which is then responded to by the server. The response could include the requested material as well as status information about the request.
HTTP Methods:
- GET
- POST
- PUT
- HEAD
- DELETE
- PATCH
- OPTIONS
GET and POST are the two most popular HTTP methods.
GET:
The Get method uses a query string to submit results. The data is linked to a URL and is accessible/visible to all users. Get is not secure but it is fast. It's typically used when you're not sending any personal information to the server, such as usernames, passwords, or credit card numbers.
- It's quick and simple, but it's not safe.
- It's the default http method.
- Since it included form data in the query string, other users can see the information.
- It uses stack method for passing form variable.
- The data is limited to the query string's maximum length.
- When data isn't sensitive, it's a great method.
- It generates easily readable URLs.
- It can only carry text data.
Example: www.website.com/Controller/Action?firstname='ABC'&&lastname='XYZ'
- namespace MvcApp.Models
- {
- public class StdModel
- {
- public int Id { get; set; }
- public string FirstName{ get; set; }
- public string LastName{ get; set; }
- }
- }
Index.cshtml View- <h3><b>HTTPGET Method</b></h3>
- @using (Html.BeginForm("GetAction", "Home", FormMethod.Get))
- {
- <table>
- <tr>
- <td>Enter ID: </td>
- <td>@Html.TextBox("Id")</td>
- </tr>
- <tr>
- <td>Enter First Name: </td>
- <td>@Html.TextBox("FirstName")</td>
- </tr>
- <tr>
- <td>Enter LastName: </td>
- <td>@Html.TextBox("LastName")</td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" value="Send"></td>
- </tr>
- </table>
- }
- <h4 style="color:purple">
- ID: @ViewBag.Id<br />
- First Name: @ViewBag.FirstName<br />
- Last Name: @ViewBag.LastName<br />
- </h4>
Index.cshtml View- <h3><b>HTTPGET Method</b></h3>
- @using (Html.BeginForm("GetAction", "Home", FormMethod.Get))
- {
- <table>
- <tr>
- <td>Enter ID: </td>
- <td>@Html.TextBox("Id")</td>
- </tr>
- <tr>
- <td>Enter First Name: </td>
- <td>@Html.TextBox("FirstName")</td>
- </tr>
- <tr>
- <td>Enter LastName: </td>
- <td>@Html.TextBox("LastName")</td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" value="Send"></td>
- </tr>
- </table>
- }
- <h4 style="color:purple">
- ID: @ViewBag.Id<br />
- First Name: @ViewBag.FirstName<br />
- Last Name: @ViewBag.LastName<br />
- </h4>
HomeController- [HttpGet]
- public ActionResult GetAction(int Id, string FirstName, string LastName)
- {
- ViewBag.Id = id;
- ViewBag.FirstName= FirstName;
- ViewBag.LastName= LastName;
- return View("Index");
- }
HomeController- [HttpGet]
- public ActionResult GetAction(int Id, string FirstName, string LastName)
- {
- ViewBag.Id = id;
- ViewBag.FirstName= FirstName;
- ViewBag.LastName= LastName;
- return View("Index");
- }
Run the application and pass value using HttpGet method, you will noticed the data is attached to URL (query string).
http://localhost:2099/Home/GetAction?Id=1&FirstName=abc&LastName=xyzPOST:
The Post method does not attach data to the URL and hides information from it. It is more stable than the Get process, but it is also slower. It's just useful if you're sending confidential data to the server.
- Data is not visible to user while using Post method
- It is more secure than Get, but it is slower.
- It uses Heap method for passing form variable.
- It has no data passing limitations and can post an infinite number of form variables.
- It's used for submitting sensitive information.
- It has the ability to hold both text and binary data.
Example: http://localhost:2099/Home/PostData
- namespace MvcForms.Models
- {
- public class StudentModel
- {
- public int Id { get; set; }
- public string FirstName{ get; set; }
- public string LastName{ get; set; }
- }
- }
Index.cshtml Form
- <h3><b>Post Method</b></h3>
- @using (Html.BeginForm("PostData", "Home", FormMethod.Post))
- {
- <table>
- <tr>
- <td>Enter ID: </td>
- <td>@Html.TextBox("Id")</td>
- </tr>
- <tr>
- <td>Enter First Name: </td>
- <td>@Html.TextBox("FirstName")</td>
- </tr>
- <tr>
- <td>Enter Last Name: </td>
- <td>@Html.TextBox("LastName")</td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" value="Send"></td>
- </tr>
- </table>
- }
- <h4 style="color:purple">
- ID: @ViewBag.Id<br />
- First Name: @ViewBag.FirstName<br />
- Last Name: @ViewBag.LastName<br />
- </h4>
HomeController.cs
- [HttpPost]
- public ActionResult PostData(FormCollection fc)
- {
- ViewBag.Id = fc["Id"];
- ViewBag.FirstName= fc["FirstName"];
- ViewBag.LastName= fc["LastName"];
- return View("Index");
- }
Difference between HttpGet and HttpPost Method:
- The Get method is the default, but if you're using the Post method to post data, you'll need to define the Post attribute
- The Get method generates a name-value pair query string, while the Post method transfers the name and value pairs in the body of the HTTP request.
- Get requests have a maximum length limit of 255 characters, while Post requests have no such restriction.
- Get is faster than Post in comparison. Encapsulating the data with Post takes a long time.
- While Get can only carry string data, Post can carry both string and binary data.
- The Get method generates a readable URL that can be cached and bookmarked, while the Post method does not.
Summary:
In this blog i tried to explain you the difference between http request methods (GET and POST), I hope now you will have an idea of get and post.
Happy Coding!
0 Comments