Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

curl request using LWP::UserAgent

by Jessarah FL (Novice)
on Oct 14, 2013 at 12:37 UTC ( #1058181=perlquestion: print w/ replies, xml ) Need Help??
Jessarah FL has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I have the following curl request that works:
curl -D- -u myUsr:myPwd -X PUT --data '{ "update": { "comment": [ {"ad +d":{"body":"new comment via rest"} } ] } }' -H "Content-Type: applica +tion/json" 'http://myjira:8080/rest/api/2/issue/TSP-3'
I would like to do the same but using LWP::UserAgent. I had tried different ways to call it, here is one:
local $ua, $key, %form, $response, $responseTxt; local %req; $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->agent("my trigger"); local $usr="myUsr"; local $pwd="myPwd"; local $reqUri="http://myjira:8080/rest/api/2/issue/TSP-3"; local $data='{ "update": { "comment": [ {"add":{"body":"new commen +t via LWP rest"} } ] } }'; $req{'json'} = $data; my $response = $ua->post( $reqUri, \%req, Authorization => 'Basic +' . encode_base64($usr . ':' . $pwd),'Content-Type'=>'application/jso +n'); if ($response->is_success) { $responseTxt=$response->decoded_content; print "\nResponse:\n$responseTxt\n"; } else { die $response->status_line; }
but I keep getting: "405 Method Not Allowed at ..."

Thanks for your enlightenment,


Comment on curl request using LWP::UserAgent
Select or Download Code
Replies are listed 'Best First'.
Re: curl request using LWP::UserAgent
by Corion (Pope) on Oct 14, 2013 at 12:57 UTC

    There are so many things that do not match between the curl call and your LWP::UserAgent code that this is hard to diagnose.

    I recommend you first make the two identical. Things to check are:

    1. The attempt to add a Content-Type header is not even valid Perl
    2. URL that curl uses and the URL you use ($reqUri)
    3. Data that gets POSTed between curl and your code
Re: curl request using LWP::UserAgent
by Jessarah FL (Novice) on Oct 14, 2013 at 14:22 UTC
    My problem is that I needed to use put method, and for that I needed to use HTTP::Request The following solutions works:
    my $uri=$reqUri; local $data='{ "update": { "comment": [ {"add":{"body":"new commen +t via rest and LWP"} } ] } }'; local $ua, $key, %form, $response; $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->agent("citrigger"); my $request = HTTP::Request->new(PUT => $uri); $request->header(Authorization => 'Basic ' . encode_base64($usr . +':' . $pwd)); $request->content_type('application/json'); $request->content($data); $response = $ua->request($request); if ($response->is_success) { $responseTxt=$response->decoded_content; print "\nResponse:\n$responseTxt\n"; } else { die $response->status_line; } return;
    regards, Jessica

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1058181]
Approved by hdb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2016-02-11 15:55 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (376 votes), past polls