Click here to get back home

Problems with STDIN and POST data

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Problems with STDIN and POST data SergioQ 02-13-2008
Get Chitika Premium
Posted by SergioQ on February 13, 2008, 2:00 pm
Please log in for more thread options
Hello all,

Am having some very serious problems reading the POST data that is
being sent to my Perl script. The full code (minus my email address
is below). And essentially what I am getting back via email reports
is that that there is data being sent via POST, and tht it has a
length of 1431. Yet all my attempts to read it are failing
miserably. If anyone can help me out, I would soooo appreciate it.

Thank you so much.

Here is the FULL CODE:

#!/usr/bin/perl

use strict;
use MainMod;
use HTTP::Request;
use LWP::UserAgent;
use CGI qw(:standard);

my $out_msg = "";
my %variable;
my $serg_bffr;

read STDIN, $serg_bffr, $ENV;

my $sendmail = "/usr/sbin/sendmail -t";
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";

print SENDMAIL ---my email address goes here---
print SENDMAIL ---my email address goes here---
print SENDMAIL "Subject: IPN Stuff\n";
print SENDMAIL "\n";
print SENDMAIL "$ENV\n";
print SENDMAIL "$ENV\n";
print SENDMAIL length($serg_bffr) . "\n";
print SENDMAIL "$serg_bffr\n";

close(SENDMAIL);

PrintItOut("done");
exit (0);

Posted by xhoster on February 13, 2008, 2:36 pm
Please log in for more thread options
> Hello all,
>
> Am having some very serious problems reading the POST data that is
> being sent to my Perl script. The full code (minus my email address
> is below). And essentially what I am getting back via email reports
> is that that there is data being sent via POST, and tht it has a
> length of 1431. Yet all my attempts to read it are failing
> miserably.

"Failing miserably" is about as bad as "doesn't work". What actually
happens?

Does your hard drive reformat itself and then your cpu set itself on fire?

> Here is the FULL CODE:
>
> #!/usr/bin/perl
>
> use strict;
> use MainMod;
> use HTTP::Request;
> use LWP::UserAgent;
> use CGI qw(:standard);

Since you are using it, why not use CGI to read from STDIN for you?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by SergioQ on February 13, 2008, 3:25 pm
Please log in for more thread options
On Feb 13, 2:36=A0pm, xhos...@gmail.com wrote:

> "Failing miserably" is about as bad as "doesn't work". =A0What actually
> happens?
I see that there is PPOST data, and it has length, but what I read
back is nothing.



> Since you are using it, why not use CGI to read from STDIN for you?


This is a Perl script for Paypal's IPN. Below is their sample
script. And you'll see there's a section "# post back to PayPal
system to validate" and since I don't know what's coming in, and in
what format, how can I tack it on and send it back?


THIS IS FROM PAYPAL
#!/usr/bin/perl

# read post from PayPal system and add 'cmd'
read (STDIN, $query, $ENV);
$query .=3D '&cmd=3D_notify-validate';

# post back to PayPal system to validate
use LWP::UserAgent;
$ua =3D new LWP::UserAgent;
$req =3D new HTTP::Request 'POST','http://www.paypal.com/cgi-bin/
webscr';
# note: if you have SSL encryption Enabled, use <https://
www.paypal.com/cgi-bin/webscr> above
$req->content_type('application/x-www-form-urlencoded');
$req->content($query);
$res =3D $ua->request($req);

# split posted variables into pairs
@pairs =3D split(/&/, $query);
$count =3D 0;
foreach $pair (@pairs) {
($name, $value) =3D split(/=3D/, $pair);
$value =3D~ tr/+/ /;
$value =3D~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$variable =3D $value;
$count++;
}

# assign posted variables to local variables
# note: additional IPN variables also available -- see IPN
documentation
$item_name =3D $variable;
$receiver_email =3D $variable;
$item_number =3D $variable;
$invoice =3D $variable;
$a =3D $variable;
$payment_status =3D $variable;
$payment_gross =3D $variable;
$txn_id =3D $variable;
$payer_email =3D $variable;
$amount =3D $variable;
$on0 =3D $variable;
$os0 =3D $variable;
$status =3D $res->content;
$note =3D $variable;


if ($res->is_error) {
# HTTP error
}
elsif ($res->content eq 'VERIFIED') {
# check the payment_status=3DCompleted
# check that txn_id has not been previously processed
# check that receiver_email is an email address in your PayPal account
# process payment
# print to screen the following if the IPN POST was VERIFIED


print "content-type: text/plain\n\nOK\n";
print "<html><head><title>IPN Screendump</title></head>\n";
print "<body>your email address is <b>$payer_email</b>\n";
print "<br>you paid <b>$payment_gross</b>\n";
print "<br>you paid for <b>$item_number</b>\n";
print "<br>the color of your order was <b>$os0</b>\n";
print "<br>the value of custom was <b>$a</b>\n";
print "<br>the status was<b>$status</b>\n";
print "<br>the note said <b>$note</b>\n";
print "<br>the transaction id was <b>$txn_id</b>\n";
print "<br>the payment status was<b>$payment_status</b>\n";
print "</body></html>\n";


}
elsif ($res->content eq 'INVALID') {
# log for manual investigation
# print to screen the following if the IPN POST was INVALID

print "content-type: text/plain\n\nOK\n";

print "<html><head><title>IPN Screendump</title></head>\n";
print "<br>the status was<b>$status</b>\n";
print "</body></html>\n";


}
else {
# error
}



Posted by xhoster on February 13, 2008, 5:30 pm
Please log in for more thread options
> On Feb 13, 2:36=A0pm, xhos...@gmail.com wrote:
>
> > "Failing miserably" is about as bad as "doesn't work". =A0What actually
> > happens?
> I see that there is PPOST data, and it has length, but what I read
> back is nothing.

Then check the return value of the "read" function and if it is undef
(as opposed to 0) see what is in $!.

Are you sure that whatever is generating these POST requests, or whatever
is proxying them to you, isn't lying about CONTENT_LENGTH?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Petr Vileta on February 14, 2008, 6:54 am
Please log in for more thread options
SergioQ wrote:
> On Feb 13, 2:36 pm, xhos...@gmail.com wrote:
>
>
> THIS IS FROM PAYPAL
> #!/usr/bin/perl
>
> # read post from PayPal system and add 'cmd'
> read (STDIN, $query, $ENV);
> $query .= '&cmd=_notify-validate';
>
Hmm, this is very stupid example on Paypel web, I know it :-)
Reading from STDIN on some hostings not work or not work properly. I rewrote
this to

#!/usr/bin/perl
use CGI qw(Vars param);
use LWP::UserAgent;

my @query='';
foreach (keys %)
{
push @query, $_ . '=' . param($_);
}
push $query, 'cmd=_notify-validate';

# post back to PayPal system to validate
$ua = new LWP::UserAgent;
$req = new HTTP::Request 'POST','http://www.paypal.com/cgi-bin/webscr';
$req->content_type('application/x-www-form-urlencoded');
$req->content(join('&', @query));
$res = $ua->request($req);

--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your
mail from another non-spammer site please.)

Please reply to <petr AT practisoft DOT cz>


Similar ThreadsPosted
Probelm to post XML data in a loop. First time XML is posted, second time data is getting truncated. Please help. February 9, 2006, 7:01 am
how to get data from POST? November 18, 2004, 8:19 pm
LWP, Post, return data ? October 11, 2007, 10:26 pm
How do I detect if I have incoming data in when I pipe somethingto my perl script ? August 5, 2004, 2:45 pm
Accessing form POST data July 24, 2004, 10:24 pm
parsing UTF-8 chars out of POST data September 8, 2004, 11:00 am
POST data with XMLHttpRequest to CGI (Mozilla) April 30, 2005, 1:55 pm
POST data truncated in a cgi application August 10, 2006, 8:36 am
dump POST data to screen February 15, 2008, 9:41 am
How to post back the data by selecting a value from drop down list November 20, 2006, 9:28 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap