Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

LWP::Useragent - File Upload

by leefp (Initiate)
on Dec 14, 2018 at 21:26 UTC ( #1227272=perlquestion: print w/replies, xml ) Need Help??

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' );

Replies are listed 'Best First'.
Re: LWP::Useragent - File Upload
by bliako (Prior) on Dec 14, 2018 at 21:41 UTC

    You are passing POST params as an array-ref, parameter name is missing. Shouldn't be a hash-ref of name=>value pairs? (according to HTTP::Request) Try  my $file = { select => "/tmp/test_image.tgz.bin"}; or whatever your parameter name is.

    bw, bliako

      Thanks bliako. But, I'm still getting a '500' return code. Clearly, it doesn't like the data being passed in that way. ------------- 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("admin:Password123!")); $ua->timeout(600); my $file = { select => "/tmp/test_image.tgz.bin"}; my $url = "https://10.248.179.31/api/rest/software_package"; my $json_obj = $json->pretty->encode ($file); my $header = 'Content-Type' => 'multipart/form-data'; 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);
        sorry...poorly formatted.
        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:Password123!")); $ua->timeout(600); my $file = { select => "/tmp/test_image.tgz.bin"}; my $url = "https://10.248.179.31/api/rest/software_package"; my $json_obj = $json->pretty->encode ($file); my $header = ['Content-Type' => 'multipart/form-data']; my $request = HTTP::Request->new("POST", $url, $header, $file); print Dumper $request; my $res = $ua->request($request);
Re: LWP::Useragent - File Upload
by poj (Abbot) on Dec 15, 2018 at 14:14 UTC

    Try using just Content_Type => 'form-data' or Content_Type => 'multipart/form-data; boundary="xYzZY"'

    #!/usr/bin/perl use strict; use warnings; use MIME::Base64; use HTTP::Request::Common; my $file = "/tmp/test_image.tgz.bin"; my $url = "https://10.248.179.31/api/rest/software_package"; my $request = HTTP::Request::Common::POST( $url, Authorization => 'Basic ' . encode_base64('user:password'), Content_Type => 'multipart/form-data; boundary="xYzZY"', Content => [ file => [$file] ], ); print $request->as_string;
    poj
      I'm already using that...See the code posted above. Thanks

        Did you try the code I posted ?

        poj

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1227272]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2020-09-21 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (125 votes). Check out past polls.

    Notices?