Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

http POST replies

by dmsparts (Sexton)
on Aug 19, 2008 at 11:49 UTC ( [id://705184]=perlquestion: print w/replies, xml ) Need Help??

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

i am trying to setup a script that will accept POST replies and write them into a text file. i can get it to work with the GET action but the website that is sending the info is using POST and cannot cahnge it, at the moment i have,
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; $from = $postFields{ "from" }; $msg = $postFields{ "text" }; $msgid = $postFields{ "msgid" }; $msg =~ s/%(..)/pack("C", hex($1))/eg; open (SMSREPLY, ">>$smsreply/$msgid.txt"); print SMSREPLY "\n$from $msg $msgid"; close SMSREPLY; } }
any help will be appreciated Michael I have now got the script working but i neet to make it retuen a HTTP 200 status back to the server that calls the script once it has executed. I cannot find any way to do this. Please help. Cheers

Replies are listed 'Best First'.
Re: http POST replies
by olus (Curate) on Aug 19, 2008 at 11:57 UTC
    use CGI; my $query = new CGI; my $msgid = $query->param('msgid'); my $from = $query->param('from'); my $msg = $query->param('msg'); # Now do what you want with the params
Re: http POST replies
by Anonymous Monk on Aug 19, 2008 at 11:56 UTC
Re: http POST replies
by zentara (Archbishop) on Aug 19, 2008 at 12:57 UTC
    It would go something like this:
    #!/usr/bin/perl use warnings; use CGI 'cgi'; my $cgi = new CGI; my $relay = ''; my %input= $cgi->Vars(); foreach $name (keys %input){ my $value = $input{$name}; $relay .= "$name=$value&"; } open (RT,">respgen.test"); print RT $relay; close RT;

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      thanks, have tried your suggestion and just gst CGI errors
        Exactly what have you tried and which errors did you get?

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: http POST replies
by SFLEX (Chaplain) on Aug 19, 2008 at 12:29 UTC
    Like everyone said above me CGI or a module alike for your form inputs, but as i look at your code your getting the file name from a form input so using taint or make sure that name does not go out of its boundary's.

    Unless you are 100% sure that only trusted people will be using that code only.

      Another way is to generate (perhaps with Data::UUID) a safe filename (ex fooBAR), and create associated metadata file (fooBAR.meta) that contains uploaded name, uploader ...
Re: http POST replies
by geira (Initiate) on Aug 19, 2008 at 12:48 UTC
    Naturally this depends on the client sending RFC-valid param=value pairs. Most web services don't, in case you ever venture into that territory.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found