Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: LWP::Useragent - File Upload

by leefp (Initiate)
on Dec 20, 2018 at 19:48 UTC ( [id://1227543]=note: print w/replies, xml ) Need Help??


in reply to Re: LWP::Useragent - File Upload
in thread LWP::Useragent - File Upload

Thanks bliako. But, I'm still getting a '500' return code.
Clearly, it doesn't like the data being passed in that way.

Replies are listed 'Best First'.
Re^3: LWP::Useragent - File Upload
by leefp (Initiate) on Dec 20, 2018 at 19:49 UTC
    The code:
    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);

      Hi leefp,

      My advice was very narrow in that it concentrated on how to be effective in doing a POST and ignored the bigger picture of uploading a file to server. I simply spotted that your POST params were in the wrong format and alerted you.

      However, for this particular case, i.e. of uploading the contents of a file, you need to send the contents of the file as multipart/form-data.

      HTTP::Request can take the name of the file (via Content => [ file => [$file] ], as poj suggested) and will take care of the rest (opening the file, encoding, etc.).

      So, try to add a header of Content like poj said.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1227543]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-24 00:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found