Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Post using LWP useragent curl post request to useragent request

by smarthacker67 (Beadle)
on May 25, 2018 at 21:31 UTC ( #1215232=perlquestion: print w/replies, xml ) Need Help??

smarthacker67 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks I am trying to create equivalent post request using LWP::Useragent
curl ENDPOINT \ -H "Authorization: Bearer ACCESS_TOKEN" -X POST \ -F attributes='{"name":"test.txt", "parent":{"id":"0"}}' \ -F file=@test.txt
my request looks like this
my $json = encode_json {"name" => "test.txt", "parent" => {"id" => 0}} +; my $resp = $ua->post( ENDPOINT , {file => <$fh> ,'attributes'=>$json }, 'Authorization' => 'Bearer ' . $token , Content_Type => 'multipart/form-data' . );
but not working getting 400 response Can some one help me. I am actually trying to upload the file to the endpoint with the curl I am able to but looking for equivalent LWP useragent request.

Replies are listed 'Best First'.
Re: Post using LWP useragent curl post request to useragent request
by ikegami (Patriarch) on May 26, 2018 at 05:32 UTC

    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 ]
      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
Re: Post using LWP useragent curl post request to useragent request
by hippo (Bishop) on May 25, 2018 at 21:52 UTC

    Your curl post uses "name":"test.txt" but your LWP post uses "name" => "upload.txt". If the datasets are different you should not be surprised that you get different results.

      It was just for sake of example. I am using the file name and passing the file content as well.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1215232]
Approved by Athanasius
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2023-06-09 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (35 votes). Check out past polls.

    Notices?