#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use MyClientRest::ClientRest;
my $host = "http://127.0.0.1:8000";
# instatiate class
my $object = new ClientRest( $host );
my $username = "user";
my $password = "password";
my $file = 'test.txt';
my $upload = '/upload/';
my %optionsFile = ( "url" => $upload,
"file" => $file,
"username" => $username,
"password" => $password );
my $postFile = $object->postSnippetsFile( %optionsFile );
print Dumper $postFile;
__END__
$ perl restClient.pl
$VAR1 = 'Not a SCALAR reference at /usr/local/share/perl/5.22.1/LWP/Protocol/http.pm line 260.
';
####
package ClientRest;
use JSON;
use Carp;
use strict;
use warnings;
use MIME::Base64;
use Data::Dumper;
use version;
our $VERSION;
$VERSION = qv('0.0.1');
use REST::Client;
sub new {
my $class = shift;
my $self = {
_host => shift,
};
_parameterValidation($self);
# instatiate Rest::Client and create constructor
my $client = REST::Client->new({
host => $self->{_host},
timeout => 10, });
$self->{_client} = $client;
bless $self, $class;
return $self;
}
sub _parameterValidation {
my( $self ) = @_;
croak "Invalid host syntax: sample 'http://:' "
unless ( $self->{_host} =~ /^http:\/\/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}$/ );
}
sub postSnippetsFile {
my ( $self, %options ) = @_;
my $headers = { "Content" => {'file' => $options{file}},
"Content-type" => 'multipart/form-data',
"Authorization" => 'Basic ' .
encode_base64($options{username} . ':' . $options{password}) };
$self->{_client}->POST( $options{url},
$headers );
return $self->{_client}->responseContent();
}
1;
####
$ curl -u user:password -H "Content-Type: multipart/form-data" -H "Accept: application/json" -H "Expect:" -F file=@test.txt -X POST http://127.0.0.1:8000/upload/ | jq '.'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 455 100 249 100 206 2547 2107 --:--:-- --:--:-- --:--:-- 2567
{
"url": "http://127.0.0.1:8000/snippets/2/",
"id": 2,
"highlight": "http://127.0.0.1:8000/snippets/2/highlight/",
"owner": "user",
"title": "Default Title",
"code": "Test POST file Perl",
"linenos": false,
"language": "perl",
"style": "emacs"
}
####
$ perl restClient.pl
$VAR1 = 'Not a SCALAR reference at /usr/local/share/perl/5.22.1/LWP/Protocol/http.pm line 260.
';
####
else {
# Set (or override) Content-Length header
my $clen = $request_headers->header('Content-Length');
if (defined($$content_ref) && length($$content_ref)) {
$has_content = length($$content_ref);
if (!defined($clen) || $clen ne $has_content) {
if (defined $clen) {
warn "Content-Length header value was wrong, fixed";
hlist_remove(\@h, 'Content-Length');
}
push(@h, 'Content-Length' => $has_content);
}
}
elsif ($clen) {
warn "Content-Length set when there is no content, fixed";
hlist_remove(\@h, 'Content-Length');
}
}
####
my $headers = { "Content" => ['file' => [$options{file}]],
"Content-type" => 'multipart/form-data',
"Authorization" => 'Basic ' .
encode_base64($options{username} . ':' . $options{password}) };
####
# module part
sub postSnippets {
my ( $self, %options ) = @_;
my $headers = { "Content-type" => 'application/json; charset=UTF-8',
"Authorization" => 'Basic ' .
encode_base64($options{username} . ':' . $options{password}) };
$self->{_client}->POST( $options{url},
encode_json($options{hashRef}),
$headers );
return decode_json $self->{_client}->responseContent();
}
# script part
my $hashRef = { "title" => "Test Title",
"code" => "print \"Test POST Request\"",
"linenos" => "false",
"language" => "perl",
"style" => "emacs" };
my %options = ( "url" => $url,
"hashRef" => $hashRef,
"username" => $username,
"password" => $password );
my $post = $object->postSnippets( %options );
print Dumper $post;
# output
$ perl restClient.pl
$VAR1 = {
'linenos' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ),
'id' => 2,
'language' => 'perl',
'code' => 'print "Test POST Request"',
'title' => 'Test Title',
'style' => 'emacs',
'highlight' => 'http://127.0.0.1:8000/snippets/2/highlight/',
'url' => 'http://127.0.0.1:8000/snippets/2/',
'owner' => 'user'
};
####
$VAR1 = '{"detail":"Multipart form parse error - Invalid boundary in multipart: None"}';
####
sub postSnippetsFile {
my ( $self, %options ) = @_;
my $headers = { "Content-type" => 'multipart/form-data',
"Content" => [ file => [$options{file}]],
"Authorization" => 'Basic '.
encode_base64($options{username} . ':' . $options{password}), };
$self->{_client}->POST( $options{url},
encode_json({file => $options{file}}),
$headers );
return $self->{_client}->responseContent();
}