Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Do you know where your variables are?
 
PerlMonks  

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,

Jessica

Comment on curl request using LWP::UserAgent
Select or Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-04-21 11:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (493 votes), past polls