Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl CGI with HTTP Status Codes

Tags:

http

w3c

cgi

perl

I have the following validation in a CGI script that will check for the GET method and return a 405 HTTP status code if the GET method is not used. Unfortunately it is still returning a 200 Status OK when using POST or PUT.

my ($buffer);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
    $buffer = $ENV{'QUERY_STRING'};
}
else
{
    $cgi->$header->status('405 Method Not Allowed')
    print $cgi->header('text/plain');
}

I am still new to CGI programming so I figured someone here could toss me a bone about working with CGI and HTTP status returns. If a good CGI doc is provided that would be awesome, as most returned by search are CPAN (already read a few times) and really old tutorials that are not Object oriented.

like image 412
MattSizzle Avatar asked Nov 09 '13 05:11

MattSizzle


People also ask

Is Perl used for CGI?

While CGI scripts can be written in many different programming languages, Perl is commonly used because of its power, its flexibility and the availability of preexisting scripts. sent to the server through CGI, the results may be sent to the client.

Is Perl CGI client side or server side?

CGI refers to server-side execution, while Java refers to client-side execution. There are certain things (like animations) that can be improved by using Java. However, you can continue to use Perl to develop server-side applications.

What is CGI linkage in Perl?

In Perl, CGI(Common Gateway Interface) is a protocol for executing scripts via web requests. It is a set of rules and standards that define how the information is exchanged between the web server and custom scripts. Earlier, scripting languages like Perl were used for writing the CGI applications.

How can I get status code from HTTP status?

To get the status code of an HTTP request made with the fetch method, access the status property on the response object. The response. status property contains the HTTP status code of the response, e.g. 200 for a successful response or 500 for a server error.


1 Answers

cpan docs is more than enought for CGI. If you want new tutorials don't use CGI, use one of MVC frameworks ( Catalyst, Dancer2, Mojo, etc ).

You can post 405 header if will change:

$cgi->$header->status('405 Method Not Allowed');
print $cgi->header('text/plain');

to this:

print $cgi->header(
   -type=>'text/plain',
   -status=> '405 Method Not Allowed'
);
like image 50
Suic Avatar answered Sep 23 '22 13:09

Suic