Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

HTTP 200 Status

by dmsparts (Sexton)
on Sep 02, 2008 at 13:28 UTC ( [id://708478]=perlquestion: print w/replies, xml ) Need Help??

dmsparts has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that accepts HTTP Post Data and writes them into a text file. . However i need the script to also send a HTTP 200 status back to the server sending the POST Data after the script has run. At the moment it is sending HTTP 502 (Bad Gateway) Can anyone Help Please, Cheers Michael Hi again here is the code so far......
sub smsreply{ $smsreply = 'd:\inetpub\wwwroot\cgi-bin\classads\test'; { local ($buffer, @pairs, $pair, $name, $value, %FORM)}; # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CON +TENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value = +~ tr/+/ /; $postFields{ "$name" } = $value; #populatePostFields; $from = $postFields{ "from" }; $msg = $postFields{ "text" }; $msgid = $postFields{ "msgid" }; open (SMSREPLY, ">>$smsreply/abc.txt"); print SMSREPLY "\n $name"; print SMSREPLY "\n $value"; close SMSREPLY; } }
Hope this helps

Replies are listed 'Best First'.
Re: HTTP 200 Status
by moritz (Cardinal) on Sep 02, 2008 at 13:44 UTC
    "Bad Gateway" means that the HTTP server forwards something to another server or a handler (like a proxy does), and that forwarding fails somehow.

    So look at your server's error log file to find out what went wrong, and fix it.

Re: HTTP 200 Status
by Fletch (Bishop) on Sep 02, 2008 at 13:36 UTC

    Well it's obvious from your copious sample code the problems on line 42. Near where you make the call to return_wrong_HTTP_status_code().

    See also How (Not) To Ask A Question.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: HTTP 200 Status
by jettero (Monsignor) on Sep 02, 2008 at 13:36 UTC
Re: HTTP 200 Status
by Anonymous Monk on Sep 02, 2008 at 14:06 UTC
      I dont exactly know what you mean. The script is a subroutine in a larger CGI script all the CGI declarations are made at the start. The script actually does what i want it to do other than send a HTTP 200 status back to the server once completed.

        What the poster means is that you should be using the CGI.pm module instead of trying to roll your own query parsing. There are numerous, well-documented reasons for this (see use CGI or die;, for example).

        As for your question, you really haven't provided enough information for anyone to give you an intelligent answer. The reason for your program's failure can't be deduced from the snippet of code you've listed.

Re: HTTP 200 Status
by Sagacity (Monk) on Sep 02, 2008 at 20:57 UTC

    This is not tested, but should get you close enough

    #!/usr/bin/perl sub emailreply{ use strict;#comment this out after debugging, but leave it here for yo +ur next adventure use CGI; my $q = CGI->new(); $q->ReadParse(); my $smsreplydir = 'd:\inetpub\wwwroot\cgi-bin\classads\test'; my $from = $q->{ "from" }; my $msg = $q->{ "text" }; my $msgid = $q->{ "msgid" }; open (SMSREPLY, ">>$smsreplydir/abc.txt"); print SMSREPLY "\n $msgid\n $from\n\n $msg\n\n"; close SMSREPLY; print $q->header(); #sends your HTTP 200 OK header to the browser, you + can control alot of this info yourself print $q->start_html();#handles the html body tag #There are more subs that can handle all of your html needs, tables, e +tc. print "Thanks for your input. The information has been forwarded to th +e Big Cheese.\n";#Change this to your liking print $q->end_html();#End the html page properly }

    Check out http://search.cpan.org/~lds/CGI.pm-3.25/CGI.pm#PRAGMAS

    The style of your original script needs to be updated to the better supported OO style that uses modules and packages

    I remember seeing this type of scripting back in the days of cgi-lib.pl

    Check to see if use CGI is already called, if it is, set a different scalar for CGI->new() instead of $q.

    You may use any scalar you want, but $q might be used elsewhere in the scripting.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://708478]
Approved by jettero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 09:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found