cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
396
Views
0
Helpful
1
Replies

Error Cisco Prime HTTP GET request

Demarco
Level 1
Level 1

I'm trying to make an HTTP GET request with Cisco Prime:

#!/opt/local/bin/perl -w

use strict;

use JSON-support_by_pp;
use LWP 5.64;
use LWP::UserAgent;

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

my $ua = LWP::UserAgent->new;

my $BASE_URL = 'https://Host_name/webacs/api/v1/';
my $UN       = "Username";
my $PW       = "Password";

sub fetch ($) {
    my ( $url ) = @_;
    my $req = HTTP::Request->new( GET => $BASE_URL . $url );
    $req->authorization_basic( $UN, $PW );
    return $ua->request( $req )->content or die( "Cannot read from " . $BASE_URL . $url );
}

my $content = fetch( 'data/AccessPoints.json?.full=true' );

my $json = new JSON;

# these are some nice json options to relax restrictions a bit:
my $json_text =
        $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode( $content );

foreach my $ap ( @{ $json_text->{queryResponse}->{'entity'} } ) {

    print "------------------------\nAccess Point " . $ap->{'accessPointsDTO'}->{'@id'} . "\n";
    print "Model:" . $ap->{'accessPointsDTO'}->{'model'} . "\n";
    print "MAC Address:" . $ap->{'accessPointsDTO'}->{'macAddress'} . "\n";
    print "Serial Number:" . $ap->{'accessPointsDTO'}->{'serialNumber'} . "\n";
    print "Software Version:" . $ap->{'accessPointsDTO'}->{'softwareVersion'} . "\n";
    print "Status:" . $ap->{'accessPointsDTO'}->{'status'} . "\n";
    print "Location:" . $ap->{'accessPointsDTO'}->{'location'} . "\n";

What do I do wrong? I have already tried with curl in shell and it works:

 curl --tlsv1 --user USER:PASSWORD--insecure https://Host_name/webacs/api/v1/data/AccessPoints.json?.full=true

I'm trying to make an HTTP GET request with Cisco Prime:

#!/opt/local/bin/perl -w

use strict;

use JSON-support_by_pp;
use LWP 5.64;
use LWP::UserAgent;

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

my $ua = LWP::UserAgent->new;

my $BASE_URL = 'https://Host_name/webacs/api/v1/';
my $UN       = "Username";
my $PW       = "Password";

sub fetch ($) {
    my ( $url ) = @_;
    my $req = HTTP::Request->new( GET => $BASE_URL . $url );
    $req->authorization_basic( $UN, $PW );
    return $ua->request( $req )->content or die( "Cannot read from " . $BASE_URL . $url );
}

my $content = fetch( 'data/AccessPoints.json?.full=true' );

my $json = new JSON;

# these are some nice json options to relax restrictions a bit:
my $json_text =
        $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode( $content );

foreach my $ap ( @{ $json_text->{queryResponse}->{'entity'} } ) {

    print "------------------------\nAccess Point " . $ap->{'accessPointsDTO'}->{'@id'} . "\n";
    print "Model:" . $ap->{'accessPointsDTO'}->{'model'} . "\n";
    print "MAC Address:" . $ap->{'accessPointsDTO'}->{'macAddress'} . "\n";
    print "Serial Number:" . $ap->{'accessPointsDTO'}->{'serialNumber'} . "\n";
    print "Software Version:" . $ap->{'accessPointsDTO'}->{'softwareVersion'} . "\n";
    print "Status:" . $ap->{'accessPointsDTO'}->{'status'} . "\n";
    print "Location:" . $ap->{'accessPointsDTO'}->{'location'} . "\n";

What do I do wrong? I have already tried with curl in shell and it works:

    curl --tlsv1 --user USER:PASSWORD--insecure https://Host_name/webacs/api/v1/data/AccessPoints.json?.full=true

I have this error:

malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "Can't connect to 10....") at ersteProbe.pl line 28.

 

 

1 Reply 1

Alex Stevenson
Cisco Employee
Cisco Employee

 

Hello @Demarco,

 

I'm not a Perl expert but I've tried my hand at debugging this and here are my observations:


1. The Perl JSON examples I've seen initialized a JSON with something like this: 

 

my $JSON = JSON->new->utf8;

 

versus your method

 

my $json = new JSON;

 

 

 

 

2. I pasted your code into VS Code and installed the Perl Toolbox for linting and syntax. Here are the two syntax errors it found:

 

A.

Perl-JSON1.png

Syntax: Possible precedence issue with control flow operator at /var/folders/yj/0nxjsy4j20n7cjwr6890cbg00000gn/T/cisco_prime-http-request.p6.syntax line 21.

 

 

B. 

Perl-JSON2.png

 

Syntax: Missing right curly or square bracket at /var/folders/yj/0nxjsy4j20n7cjwr6890cbg00000gn/T/cisco_prime-http-request.p6.syntax line 40, at end of line

 

Syntax: syntax error at /var/folders/yj/0nxjsy4j20n7cjwr6890cbg00000gn/T/cisco_prime-http-request.p6.syntax line 40, at EOF

 

 

Hope this helps!