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


in reply to Re: Post using LWP useragent curl post request to useragent request
in thread Post using LWP useragent curl post request to useragent request

Thanks for reply. I tried following your method and referred the following as well http://search.cpan.org/~oalders/HTTP-Message-6.16/lib/HTTP/Request.pm#EXAMPLES but no luck so far. still getting 405 error code. I am not sure on how to add BODY PARAMS and the actual file content with the post request. I am intending to UPLOAD a file via post request to END POINTS
  • Comment on Re^2: Post using LWP useragent curl post request to useragent request

Replies are listed 'Best First'.
Re^3: Post using LWP useragent curl post request to useragent request
by poj (Abbot) on May 26, 2018 at 15:10 UTC
    I tried following your method

    Post the code you tried

    poj
      my $request = HTTP::Request::Common::POST( BOX, [ Content_Type => 'form-data', Authorization => "Bearer $token", Content => [ attributes => $c, #encoded json file => [ "$Bin/test.txt" ], #file_path ], ] );
      __________________________try 2_______________________________
      sub build_json_request { my ($url) = @_; my $header = ['Authorization' => "Bearer $token", 'Content-Type' = +> 'form-data']; return HTTP::Request->new('POST', $url, $header, {attributes => $c +, file=> [ "$Bin/upload.bak" ],} ); } my $r = build_json_request (ENDPOINT ); my $res = $ua->request($r);
      I am pro when comes to Perl but don't know how http post works. Do I have to encode body params while passing. and not sure if LWP::Useragent will fetch the file contain itself given the full file path or not. Can you help me in get this working.
        not sure if LWP::Useragent will fetch the file contain itself

        Yes, it will. Try

        #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use JSON; use FindBin qw($Bin);; my $token = ''; my $url = 'http://'; my $attributes = encode_json ({ name => "test.txt", parent => { id => 0 } }); my $ua = LWP::UserAgent->new; my $response = $ua->post($url, Content_Type => 'form-data', Authorization => "Bearer $token", Content => [ attributes => $attributes, file => [ "$Bin/test.txt" ], ], ); if ($response->is_success) { print $response->decoded_content; } else { die $response->status_line; }
        poj