Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

PERL REST API Post script

by ptone (Initiate)
on Feb 10, 2020 at 20:51 UTC ( #11112743=perlquestion: print w/replies, xml ) Need Help??

ptone has asked for the wisdom of the Perl Monks concerning the following question:

Hi Team, I am trying to use arguments to pass information from the command line into a perl script. These are the IP, Email, the User and the password. The IP works but the rest does not get parsed into the req for the content. If I hardcode these the script works.
##!/usr/bin/perl -l use strict; use warnings; use LWP::UserAgent; my $username = "Polycom"; my $password = "789"; my $phoneip = $ARGV[0]; my $user = $ARGV[1]; my $Address = $ARGV[2]; my $passcode = $ARGV[3]; my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 }, protocols_allowed => ['https'], ); my $URL = "https://$phoneip/api/v1/mgmt/skype/signIn"; { # Create the request object and add the authentication header and cont +ent my $req = HTTP::Request->new(POST => $URL); $req->content_type('application/json'); $req->authorization_basic( $username, $password ); $req->content('{"data":{"Address": "$Address","User": "$user","Passw +ord": "$passcode","Domain":"","LockCode":""}}'); # Send the request to the user agent and print the result my $response = $ua->request($req); print "\r\n"; print $response->decoded_content; }
With the above, I am trying to help one of our customers to remotely sign into a Phone but I cannot parse the info. It must be something little but I am stuck. Could someone kindly help me out? I actually managed the same in Power Shell but I like to offer various examples to them. Best Regards Steffen

Replies are listed 'Best First'.
Re: Perl REST API Post script
by hippo (Chancellor) on Feb 10, 2020 at 23:11 UTC
    The IP works but the rest does not get parsed into the req for the content.
    $req->content('{"data":{"Address": "$Address","User": "$user","Passwor +d": "$passcode","Domain":"","LockCode":""}}');

    Single quotes don't interpolate. See Quote and Quote-like Operators in perlop for more info.

Re: PERL REST API Post script
by tangent (Vicar) on Feb 10, 2020 at 21:51 UTC
    Not an answer, but, when writing command line scripts I find using the core module Getopt::Long to parse options much more flexible (and easier to debug). For your script:
    use Getopt::Long; my ( $phoneip, $user, $Address, $passcode, ); GetOptions( 'phoneip=s' => \$phoneip, 'user=s' => \$user, 'Address=s' => \$Address, 'passcode=s' => \$passcode, );
    You would then call it with the arguments in any order, and you can easily add options:
    perl script.pl --user username --phoneip 201.34.56.78
Re: PERL REST API Post script
by haukex (Bishop) on Feb 10, 2020 at 21:09 UTC
    I am trying to use arguments to pass information from the command line into a perl script. These are the IP, Email, the User and the password. The IP works but the rest does not get parsed into the req for the content. If I hardcode these the script works.

    Use either Data::Dumper with $Data::Dumper::Useqq=1; or Data::Dump to inspect the variables you're getting from the command line, and check that they are as you expect. (Also, it looks like you're generating JSON by pasting a string together, I'd very much recommend one of the JSON modules instead, such as Cpanel::JSON::XS.)

Re: PERL REST API Post script
by kcott (Bishop) on Feb 11, 2020 at 07:44 UTC

    G'day ptone,

    Welcome to the Monastery.

    Building upon what ++hippo pointed out, consider using qq for your outermost quotes.

    You're currently producing JSON like this:

    $ perl -E 'my $x = "YZ"; say q{{"X":"$x"}}' {"X":"$x"}

    I expect you want JSON more like this:

    $ perl -E 'my $x = "YZ"; say qq{{"X":"$x"}}' {"X":"YZ"}

    — Ken

Re: PERL REST API Post script
by Anonymous Monk on Feb 11, 2020 at 16:44 UTC

    If your JSON doesn't parse, actually examining the generated JSON and figuring out why it doesn't parse is probably the way to go.

    Of course, by hand-rolling your JSON you are trying to re-invent the wheel. Why not just build the Perl structure and then have something like the JSON module do the heavy lifting for you?

    OK, JSON is not a core module, and maybe you are one of those unfortunates who can not install the module of your choice where you need it. You might still use the JSON module to help validate your hand-generated (yukkk!) JSON.

      OK, JSON is not a core module

      JSON::PP has been a core module since Perl v5.14 (2011).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2020-08-09 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (55 votes). Check out past polls.

    Notices?