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

I am trying to use json data as parameters in a mysql store procedure. I am getting a 502 error when i send my post to my perl script. If I manually enter the parameters in my script it works, so i have narrowed down my issue to $sth->execute($input->{to}, $input->{from}, $input->{id}, $input->{body});

use strict; use warnings; # # need CGI, JSON, # MIME::LITE for email, # and DBI/DBD::mysql for DB operations. # use CGI (); use JSON (); use DBI; use DBD::mysql; use MIME::Lite; # # Declare variables # my ($status, $email, $input); my $q = CGI->new; my $json = JSON->new->utf8; if ($q->param('POSTDATA')) { $input = $json->decode( $q->param('POSTDATA') ); } else { &SimpleJSONResponse("error","no JSON data found"); exit(0); } # # test that all sms values exist # if not, return error # unless (($input->{to}) && ($input->{from}) && ($input->{body}) && ($in +put->{id})) { # # if we don't get the required fields from flowroute, # return an error # &SimpleJSONResponse("error","required fields not present"); } else { # # we received the required fields from flowroute # connect to database # my $data_source = "DBI:mysql:database=flowroute;host=localhost"; my $user = "user"; my $password = "password"; my $dbh = DBI->connect($data_source, $user, $password) or die &Simp +leJSONResponse("error","cannot connect to db"); # # run stored procedure add_sms # stores sms to database and returns the email address # my $sql = "call add_sms(?, ?, ?, ?)"; my $sth = $dbh->prepare($sql) or die &SimpleJSONResponse("error","c +annot prepare sql"); $sth->execute($input->{to}, $input->{from}, $input->{id}, $input->{ +body}); } # # Subroutines # sub SimpleJSONResponse() { my($key, $value) = @_; print $q->header(-type => "application/json", -charset => "utf-8"); print $json->encode({ $key => $value }); }

Replies are listed 'Best First'.
Re: using json post in mysql stored procedure
by stevieb (Canon) on Jan 24, 2017 at 01:45 UTC

    I don't have the backend to really test this on at the moment, but a quick thought... try printing out your data before you get into the whole unless() thing:

    # at the top of the script use Data::Dumper; ... # later print Dumper $input; # or, sometimes more effective to ensure # that each var doesn't have newlines or other # nonsense for (qw(to from body)){ print "\n\n>$input->{$_}<\n\n"; } unless (($input->{to}) && ($input->{from}) && ($input->{body}) && ($in +put->{id})) {

    ...that'll show the specific var with a '>' before the text, and a '<' immediately after it. Say $input->{to} was steveb@cpan.org, it would (ie. should) show:

    >steveb@cpan.org<

    If you get no output because it's a webapp, open a file and dump the info:

    open my $wfh, '>', '/home/user/debug.txt' or die $!; for (qw(to from body)){ print $wfh "\n\n>$input->{$_}<\n\n"; }

    Rule 1: always ensure that the data you're using is exactly as you expect to be.

    Also, when dealing with a web server, check the server's error logs; they often have valuable info for wtf went sideways.

      Thank you for the prompt reply. With your suggestion I realized that the id column in the database was not long enough.