Search code examples
perl

Execute a simple API using perl script


I'm trying to execute a API using a perl script. I'm in initial stage and below is the simple script I got which is not working. I have used get command to execute the API. But I'm not sure it will work or not. In fact I'm not able to find the get command will work not because I'm getting a different error in this code . This script gives me error like below

Use of uninitialized value $api_content in print at igmp.pl line 11.

Here is the script.

#!/usr/bin/perl   
use strict; 
use warnings;  
use LWP::Simple;  

my $api_content;

my $api = 'https://admin:[email protected]/set_param?init.snmp_ver=3&mib.save'; 
$api_content = get($api); 

print $api_content;

Please let me know if there is a command is will be useful for executing this simple API. Basically I need to set the value of snmp to 3 and save.


Solution

  • I think your problem is, that you need to use LWP::UserAgent directly so you can see the details of the response:

    my $ua = LWP::UserAgent->new;
    my $response = $ua->get('https://admin:[email protected]/set_param?init.snmp_ver=3&mib.save');
    
    if ($response->is_success) {
        print $response->decoded_content;  # or whatever
    }
    else {
        die $response->status_line;
    }