Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by ikegami (Patriarch)
on May 26, 2018 at 05:32 UTC ( [id://1215240]=note: print w/replies, xml ) Need Help??


in reply to Post using LWP useragent curl post request to useragent request

For starters, that syntax doesn't match any of the documented calling conventions:

POST $url POST $url, Header => Value,... POST $url, $form_ref, Header => Value,... POST $url, Header => Value,..., Content => $form_ref POST $url, Header => Value,..., Content => $content

You want

my $attributes = { name => "test.txt", parent => { id => 0 }}; $ua->post(ENDPOINT, Content_Type => 'form-data', Authorization => "Bearer $token", Content => [ attributes => encode_json($attributes), file => [ "test.txt" ], ], );

Use the following if you don't want to read from disk:

[ undef, "test.txt", Content => $file_contents ]

Replies are listed 'Best First'.
Re^2: Post using LWP useragent curl post request to useragent request
by smarthacker67 (Beadle) on May 26, 2018 at 13:08 UTC
    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
      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found