Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
In web development, GET
and POST
are two of the most common HTTP methods used for sending data from a client to a server. They are particularly relevant when working with CGI (Common Gateway Interface) scripts, as these scripts often interact with web forms and handle data sent by browsers.
Mechanism: With GET
, data is sent as part of the URL, usually as name-value pairs appended after a ?
character.
Visibility: The data is visible in the URL. For instance, http://example.com/form.cgi?name=John&age=30
.
Limitations: There's a length limit to the amount of data you can send due to URL length restrictions imposed by browsers and servers.
Idempotence: GET
requests are intended to be idempotent, meaning multiple identical requests should have the same effect as a single request.
Usage: Typically used for fetching data without side-effects. It should not be used for operations that change data or have other side effects.
Caching: Browsers often cache GET
requests, so it's important not to use them for sensitive or changing data.
Mechanism: With POST
, data is sent in the body of the HTTP request, not in the URL.
Visibility: The data is not visible in the URL, which makes POST
more secure for sending sensitive data like passwords.
Limitations: There's no practical limitation to the amount of data you can send with POST
(apart from server and client configuration).
Idempotence: POST
requests are not idempotent. Repeating a POST
request might have different effects.
Usage: Typically used for submitting form data or uploading a file. It's also used when the request will change data on the server, e.g., updating a database.
Caching: POST
requests are generally not cached by browsers unless explicitly told to do so.
In a Perl CGI script, you can use the CGI
module to handle both GET
and POST
requests:
use CGI; my $cgi = CGI->new; # Fetching parameters regardless of method (GET or POST) my $name = $cgi->param('name'); my $age = $cgi->param('age');
Use GET
for:
Use POST
for:
While both GET
and POST
methods allow you to send data from a client to a server, they do so in different ways and are suited to different use cases. When designing a web application or a CGI script, it's crucial to understand these differences and choose the appropriate method for each task.
GET vs POST in CGI:
<!-- HTML form using GET method --> <form action="script.cgi" method="GET"> <!-- form fields go here --> <input type="submit" value="Submit"> </form> <!-- HTML form using POST method --> <form action="script.cgi" method="POST"> <!-- form fields go here --> <input type="submit" value="Submit"> </form>
CGI GET and POST methods difference:
<!-- HTML form using GET method --> <form action="script.cgi" method="GET"> <!-- form fields go here --> <input type="submit" value="Submit"> </form> <!-- HTML form using POST method --> <form action="script.cgi" method="POST"> <!-- form fields go here --> <input type="submit" value="Submit"> </form>
When to use GET or POST in CGI:
<!-- Use GET for non-sensitive data --> <form action="search.cgi" method="GET"> <!-- search input goes here --> <input type="submit" value="Search"> </form> <!-- Use POST for sensitive or large data --> <form action="login.cgi" method="POST"> <!-- login credentials go here --> <input type="submit" value="Login"> </form>
CGI query parameters in GET vs POST:
?key1=value1&key2=value2
). In POST, parameters are sent in the request body.# CGI script (script.cgi) use CGI; my $cgi = CGI->new(); # Accessing GET parameters my $param_value = $cgi->param('key');
Security considerations for CGI GET requests:
<!-- Avoid sending sensitive data via GET --> <form action="password-reset.cgi" method="GET"> <!-- password reset inputs go here --> <input type="submit" value="Reset Password"> </form>
CGI data submission with GET and POST:
<!-- Using GET to submit data --> <form action="process.cgi" method="GET"> <!-- form fields go here --> <input type="submit" value="Submit"> </form> <!-- Using POST to submit data --> <form action="process.cgi" method="POST"> <!-- form fields go here --> <input type="submit" value="Submit"> </form>
GET and POST in HTML forms CGI:
method
attribute to specify GET or POST for data submission to CGI scripts.<!-- HTML form using GET method --> <form action="script.cgi" method="GET"> <!-- form fields go here --> <input type="submit" value="Submit"> </form> <!-- HTML form using POST method --> <form action="script.cgi" method="POST"> <!-- form fields go here --> <input type="submit" value="Submit"> </form>
Handling large data with POST in CGI:
# CGI script (upload.cgi) use CGI; my $cgi = CGI->new(); # Accessing uploaded file in POST my $file_handle = $cgi->upload('file_upload');
Choosing between GET and POST in CGI scripts:
<!-- Use GET for simple queries --> <form action="search.cgi" method="GET"> <!-- search input goes here --> <input type="submit" value="Search"> </form> <!-- Use POST for sensitive or large data --> <form action="login.cgi" method="POST"> <!-- login credentials go here --> <input type="submit" value="Login"> </form>
CGI URL parameters vs form submission:
method
attribute.<!-- Using URL parameters with GET --> <a href="profile.cgi?user_id=123">View Profile</a> <!-- Using form submission with POST --> <form action="update-profile.cgi" method="POST"> <!-- form fields go here --> <input type="submit" value="Update Profile"> </form>