Downloading a Perl File Using HTTP :: Request

I am using an existing framework to automate some apis. This structure uses the HTTP :: Request module. I need to write a script to upload a file. I can do this using the HTTP :: Request :: Common module, but NOT with the Http :: Request module. But I need to use Http :: Request only for this. Below code snippets:

Using HTTP :: Request :: Common \ This Works

$request = POST $uri, Content_Type => 'multipart/form-data', Content => [ file => [$file] ] ; my $results=$ua->request($request ) ; 

Using HTTP :: Request \ This does not work, I get an error

  my $req = HTTP::Request->new("POST", $uri ); $req->header(Content_Type => "form-data"); $req->content('file=>$file'); my $res = $ua->request($req); 

Can someone please tell me what I am doing wrong in the above code?

+4
source share
1 answer

Unfortunately, much more is happening in the POST method than just wrapping the constructor of the HTTP::Request object (see here ). Including at least the following (from quick code scanning):

  • changing the header Content-Type multipart/form-data with a random boundary to indicate where the file data begins in the request
  • setting the Content-Disposition header to the appropriate value
  • reading a file and saving the contents of the file in the request body

I highly recommend not trying to do everything manually above, but it’s hard to know your exact limitations and why you cannot use HTTP::Request::Common .

+6
source

Source: https://habr.com/ru/post/1438882/


All Articles