Работа с POST запросами

Простенький пример как работать с POST запросами в Perl.

#!/usr/bin/perl -w

use strict;
use LWP::UserAgent;
use CGI::Fast;

print "Content-type:text/html\n\n";

my $dir="/var/www/broadlink/codes";
my $url = "http://192.168.1.23:7474";

while (my $q = CGI::Fast->new)
  {
  main($q);
  }

sub main
{
  my $q = shift;
  my $device = $q->param("device");
  my @url_p = ('init.d2l','choice.d2l',$device.".cbe");
  if ($device)
  {
  foreach (@url_p) {posts($_);}
  }
}

sub posts
{
  open ( FH, "$dir/$_[0]" ) or die $!;
  my @file_content = <FH>;
  my $file_content = join '', @file_content;
  close(FH);

  my $ua = LWP::UserAgent->new;
  my $req = HTTP::Request->new(POST=>$url);
  $req->content_type('application/json');
  $req->content($file_content);
  print "$dir/$_[0]\n";
  my $res = $ua->request($request);
  if ($res->is_success) {
  print "Content-type: text/html\n\n";
  print $res->content;
  }
  else {
          print "Content-type: text/html\n\n";
          print $res->status_line, "\n";
          }
}

 

 

Обновлено 06.04.2016 20:15

unix-way