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

Re^3: Post using LWP useragent curl post request to useragent request

by poj (Abbot)
on May 26, 2018 at 15:10 UTC ( [id://1215251]=note: print w/replies, xml ) Need Help??


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

I tried following your method

Post the code you tried

poj
  • Comment on Re^3: Post using LWP useragent curl post request to useragent request

Replies are listed 'Best First'.
Re^4: Post using LWP useragent curl post request to useragent request
by smarthacker67 (Beadle) on May 26, 2018 at 20:47 UTC
    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
        OMG it works like charm. I still dont know why I was complicating the things. This code block works perfectly for me. Thanks you so much @poj I got stuck for like couple of days on this issue. Now I am good to go ahead its part of CPAN module I am creating. once again thanks for your help.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found