Calling RESTful from PERL

Posted on
Mon Dec 07, 2009 2:01 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Calling RESTful from PERL

PERL Gurus out there... I can use the RESTful API to change a variable from my browser. But, when I place the same URL in a PERL script, it fails. Here is the script
Code: Select all
#!/usr/bin/perl

use LWP::UserAgent;
use HTTP::Request;

my $URL = 'http://127.0.0.1:8176/variables/foo?_method=put&value=bar';

my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 30);
my $header = HTTP::Request->new(GET => $URL);
my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $agent->request($request);

# Check the outcome of the response
if ($response->is_success){
print "URL:$URL\nHeaders:\n";
print $response->headers_as_string;
print "\nContent:\n";
print $response->as_string;
}elsif ($response->is_error){
print "Error:$URL\n";
print $response->error_as_HTML;
}
and running it produce this result
Code: Select all
Error:http://127.0.0.1:8176/variables/foo?_method=put&value=bar
<html>
<head><title>An Error Occurred</title></head>
<body>
<h1>An Error Occurred</h1>
<p>401 Unauthorized</p>
</body>
</html>
Any ideas as to the cause of the 402 error? I did try adding my username:password to the URL, but that didn't work either.

Posted on
Mon Dec 07, 2009 12:45 pm
seanadams offline
Posts: 489
Joined: Mar 19, 2008
Location: Saratoga, CA

(No subject)

Error 401 means you sent no login/pw at all. I think this is what you're looking for:

http://lwp.interglacial.com/ch11_03.htm
http://stackoverflow.com/questions/1799 ... tials-work

realm should be "Indigo Control Server"

Posted on
Mon Dec 07, 2009 1:08 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

(No subject)

the most knowledgeable seanadams wrote:
...realm should be "Indigo Control Server"
That was the key, Thanks again, you are the master of PERL.

Posted on
Mon Dec 07, 2009 4:25 pm
chrisla23 offline
Posts: 97
Joined: Sep 16, 2009

(No subject)

Short of writing it in perl/etc (already done that), is there a way to a toggle from the Restful URLs?

Thanks,

-Chris

Posted on
Mon Dec 07, 2009 8:04 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

(No subject)

Here is the RESTful API Wiki article with examples. The 5th code section example shows how to turn a device ON and OFF using the isOn attribute. There isn't a way to toggle specifically, but if you know or get the current isOn state you can negate it.

Image

Posted on
Mon Dec 07, 2009 9:17 pm
seanadams offline
Posts: 489
Joined: Mar 19, 2008
Location: Saratoga, CA

(No subject)

chrisla23 wrote:
Short of writing it in perl/etc (already done that), is there a way to a toggle from the Restful URLs?


You could call an action group to do it

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests

cron