http://qs321.pair.com?node_id=1227272

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

I'm trying to upload a file, via REST. but I keep getting a 500 Not a SCALAR reference I'm read numerous articles as to why...I can only speculate that my data/content is not correctly formatted.
use strict; use warnings; use diagnostics; use IO::Socket::SSL qw( SSL_VERIFY_NONE ); use LWP::UserAgent; use Data::Dumper; use MIME::Base64; use JSON; my $json = JSON->new; my $ua = LWP::UserAgent->new(); $ua->ssl_opts( SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE +, SSL_hostname => '', verify_hostname => 0 ); $ua->default_header('Content-Type' => 'application/json' ); $ua->default_header('Content_Type' => 'form-data;boundary=xYzZY'); $ua->default_header('Authorization', "Basic " . encode_base64("adm +in:password")); $ua->timeout(600); my $file = ["/tmp/test_image.tgz.bin"]; my $url = "https://10.248.179.31/api/rest/software_package"; my $header = ['Content-Type' => 'form-data;boundary=xYzZY']; my $request = HTTP::Request->new("POST", "$url", $header, $file); # my $request = HTTP::Request->new("GET", "${url}?select=*", undef) +; print Dumper $request; my $res = $ua->request($request); if($res->is_success) { my $data = $json->decode($res->decoded_content); print Dumper $data; } else { print Dumper $res; }
Here is what the request looks like...
$VAR1 = bless( { '_headers' => bless( { 'content-type' => 'form-data;b +oundary=xYzZY' }, 'HTTP::Headers' ), '_uri' => bless( do{\(my $o = 'https://10.248.179.31/ +api/rest/software_package')}, 'URI::https' ), '_content' => [ '/tmp/test_image.tgz.bin' ], '_method' => 'POST' }, 'HTTP::Request' );
And here is the response
$VAR1 = bless( { '_headers' => bless( { 'content-type' => 'text/plain' +, 'client-warning' => 'Internal +response', '::std_case' => { 'client-date +' => 'Client-Date', 'client-warn +ing' => 'Client-Warning' }, 'client-date' => 'Fri, 14 Dec +2018 21:18:16 GMT' }, 'HTTP::Headers' ), '_rc' => 500, '_request' => bless( { '_headers' => bless( { 'author +ization' => 'Basic YWRtaW46UGFzc3dvcmQxMjMh ', '::std_ +case' => { + 'if-ssl-cert-subject' => 'If-SSL-Cert-Subject' + }, 'conten +t-type' => 'form-data;boundary=xYzZY', 'user-a +gent' => 'libwww-perl/6.26' }, 'HTTP: +:Headers' ), '_uri' => bless( do{\(my $o = +'https://10.248.179.31/api/rest/software_package')}, 'URI::https' ), '_content' => [ '/tmp/test_ima +ge.tgz.bin' ], '_method' => 'POST' }, 'HTTP::Request' ), '_content' => 'Not a SCALAR reference at /usr/lib/per +l5/vendor_perl/5.18.2/LWP/Protocol/http.pm line 256. ', '_msg' => 'Not a SCALAR reference' }, 'HTTP::Response' );