Navratri Sale: Get Upto 30% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Difference between Http Get and Http Post Methods

Difference between Http Get and Http Post Methods

01 Jun 2024
Intermediate
108K Views
19 min read

Difference between Http Get and Http Post Methods: An Overview

HttpGET and HttpPost methods are two commonly used methods within HTTP where the Hypertext Transfer Protocol (HTTP) is a communication protocol that is designed to enable request-response between clients and servers. Here, a web browser is the client and an application on a computer that hosts a web site is the server. In this Tutorial, we'll learn about HttpGET and HttpPOST method and Difference between httpGET and httpPOST methods, Comparison between httpGET vs httpPOST. Enhance your knowledge of ASP.NET with us through ASP.NET Certification Training.

Read More: Top 50 ASP.NET Core Interview Questions and Answers for 2024

Http GET method

Take an html form named "get_form.htm" and write the following code.

<html>
<head>
<title>Using Http Get Method</title>
</head>
<body>
<form id="frm_get" action=" Receiving_Get_Form.aspx" target="_blank" method="GET" >
<table>
<tr>
<td>First Name : </td> <td><input type="text" id="txtF_Name" name="F_name" /></td>
</tr> <tr>
<td>Last Name : </td> <td><input type=" text" id="txtL_name" name="L_name" /></td>
</tr> <tr>
<td>Email-Id : </td> <td><input type="text" id="txtE_mail" name="E_mail" /></td>
</tr> <tr>
<td>Password: </td> <td><input type="password" id="txtP_word" name="P_word"/> </td>
</tr> <tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
 </form> </body>
</html>

When we click submit button of this form, it will be redirected to "Receiving_Get_Form.aspx" in a new window.

Page.Request.QueryString["param_name"]

<%@ page language="C#" AutoEventWireup="true" codeFile="Receiving_ Get_Form.aspx.cs" Inherits="Receiving_ Get_Form"% >
<html>
<head>
<title>Data Received Here </title>
</head>
<body>
<table border="1" cellpadding="6" cellspacing="3" >
<tr>
<td>First Name : </td> <td> <% Response.Write(Page.Request.QueryString["F_name"]); %> </td>
</tr>
<tr>
<td>Last Name : </td> <td> <% Response.Write(Page.Request.QueryString["L_name"]); %> </td>
</tr>
<tr>
<td>Email-Id : </td> <td> <% Response.Write(Page.Request.QueryString["E_mail"]); %> </td>
</tr>
<tr>
<td>Password : </td> <td> <% Response.Write(Page.Request.QueryString["P_word"]); %> </td>
</tr>
</table>
</body>
</html>

The First Name, Last Name, Email-Id and Password text boxes of get_form.htm form will be sent as parameter of query string with the field name are F_name, F_name, E_mail and P_word respectively. The name of query string fields automatically taken from name attribute of each HTML element, so don’t forget to specify the name attribute So that values should be retrieving using Page.Request.QueryString ["param_name"] here in "Receiving_Get_Form.aspx" automatically.

Key points about data submitted by using HttpGet

  1. GET - Requests data from a specified resource

  2. An hyperlink or anchor tag that points to an action will ALWAYS be an HttpGet.

  3. Data is submitted as a part of url.

  4. Data is visible to the user as it posts as query string.

  5. It is not secure but fast and quick.

  6. It use Stack method for passing form variable.

  7. Data is limited to max length of query string.

  8. It is good when you want user to bookmark page.

Read More: How to start your career as ASP.NET developer

HttpPost method

The POST request method is designed to request that a web server accepts the data enclosed in the request message's body for storage

Take an html form named “post_form.htm" and write the following code.

</head>
<body>
<form id="frm_post" action=" Receiving_Post_Form.aspx" target="_blank" method=" POST" >
<table>
<tr>
 <td>First Name : </td> <td><input type="text" id="txtF_Name" name="F_name" /></td>
</tr> <tr>
<td>Last Name : </td> <td><input type=" text" id="txtL_name" name="L_name" /></td>
</tr> <tr>
 <td>Email-Id : </td> <td><input type="text" id="txtE_mail" name="E_mail" /></td>
</tr> <tr>
 <td>Password: </td> <td><input type="password" id="txtP_word" name="P_word"/> </td>
</tr> <tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
 </form> </body>
</html>

When we click submit button of this form, it will be redirected to "Receiving_Post_Form.aspx" (opened in new window too). In ASP.NET, if data passed through HTTP Post method we need the following code to retrieve the data (as written in "Receiving_Post_Form.aspx").

Page.Request.Form["param_name"]

<%@ page language="C#" AutoEventWireup="true" codeFile="Receiving_Post_Form.aspx.cs" Inherits=" Receiving_Post_Form"% >
<html>
<head>
<title>Data Received Here </title>
</head>
<body>
<table border="1" cellpadding="6" cellspacing="3" >
<tr>
<td>First Name : </td> <td> <% Response.Write(Page.Request.Form["F_name"]); %> </td>
</tr>
<tr>
<td>Last Name : </td> <td> <% Response.Write(Page.Request.Form["L_name"]); %> </td>
</tr>
<tr>
<td>Email-Id : </td> <td> <% Response.Write(Page.Request. Form["E_mail"]); %> </td>
</tr>
<tr>
<td>Password : </td> <td> <% Response.Write(Page.Request. Form["P_word"]); %> </td>
</tr>
</table>
</body>
</html>

Values of "post_form.htm"(that used method="POST") form sent using POST method therefore, the URL still intact, we retrieve The First Name, Last Name, Email-Id and Password text using Page.Request.Form["param_name"] value taken from the name each of HTML element of second form (Once again don’t forget to specify name attribute for all textboxes so that we are able to get value automatically here in this page). When you click Submit button, values of "post_form.htm" passed to "Receiving_Post_Form.aspx".

Key points about data submitted using HttpPost
  1. POST - Submits data to be processed to a specified resource

  2. A Submit button will always initiate an HttpPost request.

  3. Data is submitted in http request body.

  4. Data is not visible in the url.

  5. It is more secured but slower as compared to GET.

  6. It use heap method for passing form variable

  7. It can post unlimited form variables.

  8. It is advisable for sending critical data which should not visible to users.

Comparison between HttpGet and HttpPost:

AspectHTTP GETHTTP POST
Data TransferIt sends data in the query string of the URL.It sends data in the body of the HTTP request.
Data LengthThe URL length restrictions also limits the data length.It can take up larger data payloads.
CachingIt can be cached easily by browsers, proxies and servers.It can not be cached easily.
SecurityThe visibility of the data in the URL makes it less secure.The data can not be seen anywhere in the URL which makes it more secure.

When should you use GET vs POST

Use GET when:

  • retrieving data from the server without modifying anything like fetching a webpage or such static resources.
  • repeating the same request that gives the same result every time like when you search products on e-commerce websites.
  • you want to bookmark or share URLs so the request gives the same result.

Use POST when:

  • submitting data to the server so as to create , update or delete the resources like submitting a form, uploading a file, etc.
  • dealing with data that is sensitive like passwords or payment information.
  • repeating the request may also change the results like submitting a form to update profile of the user.
Summary

I hope you will enjoy the tips. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. To increase your command in ASP.NET, consider enrolling in our ASP.NET Certification Course and get your hands on a comprehensive step by step guide to understand the core concepts of ASP.NET with ease.

FAQs

Q1. What is the difference between HTTP POST and GET?

The main difference between HTTP POST and GET is that POST keeps the data hidden by sending it in the body of the request whereas GET makes the data visible in the URL as it sends the data with the query string of the URL.

Q2. What is the difference between HTTP POST and put?

The difference between HTTP POST and PUT is that POST is used to pass the data to the server which creates a new resource, while PUT is used to just update or replace a resource that already exists on the server.

Q3. Why HTTP POST is used?

HTTP POST is typically used to take data from the client and submit it to a server for processing which also involves the modification of the server side data.

Q4. Which is more secure HTTP GET or POST?

HTTP POST is more secure as it sends the data in the request body making it invisible in the URL unlike HTTP GET which will make the data visible as it sends it in the query string of the URL.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this