http://qs321.pair.com?node_id=1204333

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

First off, I'll be honest, I don't know anything about Perl other than it's a powerful language. I'm trying to provide sample code to "Perl" programmers to use an API.

In C# this is the code..
try { string apiKey = "Your Secret Key"; string emailToValidate = "example@example.com"; string responseString = ""; string apiURL = "https://api.zerobounce.net/v1/validate?apikey=" + +apiKey + "&email=" + HttpUtility.UrlEncode(emailToValidate); //Uncomment out to use the optional API with IP Lookup //string apiURL = "https://api.zerobounce.net/v1/validatewithip?api +key=" + apiKey + "&email=" + HttpUility.UrlEncode(emailToValidate); ++ "&ipaddress=" + HttpUtility.UrlEncode("99.123.12.122") HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiURL); request.Timeout = 150000; request.Method = "GET"; using (WebResponse response = request.GetResponse()) { response.GetResponseStream().ReadTimeout = 20000; using (StreamReader ostream = new StreamReader(response.GetRe +sponseStream())) { responseString = ostream.ReadToEnd(); } } } catch (exception ex) { Catch Exception - All errors will be shown here - if there are iss +ues with the API }
How would the above be converted to PERL? I seen a few different examples on the web (Might be out of date), but I don't have an easy way to test things. If some awesome monk can give me the basic syntax and then point me to an online Perl compiler that allows JSON REST Calls, I can probably take it from there.

Replies are listed 'Best First'.
Re: Help me convert this to Perl
by haukex (Archbishop) on Nov 27, 2017 at 17:11 UTC

    Well, normally this isn't a code writing service, but if it helps Perl programmers, I guess it's for a good cause ;-) Will this documentation be public somewhere?

    If I understood the C# code correctly, here's one way to do it with HTTP::Tiny and URI (for SSL support, IO::Socket::SSL and Net::SSLeay need to be installed too):

    #!/usr/bin/env perl use warnings; use strict; use URI; use HTTP::Tiny; my $uri = URI->new('https://api.zerobounce.net/v1/validate'); $uri->query_form( apikey => 'Your Secret Key', email => 'example@example.com', #ipaddress => '99.123.12.122', ); my $response = HTTP::Tiny->new->get( $uri, { timeout=>15 } ); # in seconds die "Failed! $response->{status} $response->{reason}\n" unless $response->{success}; my $responseString = $response->{content};
    an online Perl compiler that allows JSON REST Calls

    I'd recommend installing Perl locally for testing (and perhaps just pointing the script at a different URL for testing). It comes pre-installed on many *NIX systems, and on Windows, Strawberry Perl makes things fairly easy. All of the aforementioned modules are included in the latest Strawberry Perl release, while on *NIX, some of them may need to be installed.

      It may help "Perl" programmers, but I think an actual Perl programmer would just look at the C# code and write a Perl program (as two actual Perl programmers have already done :).


      Give a man a fish:  <%-{-{-{-<

        Yeah, really a REST API document only needs to show the routes and parameters, and the data returned ... not a client implementation. Honestly that would get in the way of seeing and grasping the needed info.


        The way forward always starts with a minimal test.
Re: Help me convert this to Perl
by 1nickt (Canon) on Nov 27, 2017 at 17:11 UTC

    Roughly:

    use strict; use warnings; use URI::Escape 'uri_escape'; use HTTP::Tiny; use Try::Tiny; my $api_key = 'Your Secret Key'; my $email = 'example@example.com'; my $url = 'https://api.zerobounce.net/v1/validate'; my $params = '?apikey=' . $apikey . '&email=' . $email; my $api_url = uri_escape( $url . $params ); my $ua = HTTP::Tiny->new( timeout => 15 ); # seconds my $response_string; my $api_error; try { my $res = $ua->get( $api_url ); if ( $res->{'success'} ) { $response_string = $res->{'content'}; } else { $api_error = $res->{'status'} . ' ' . $res->{'reason'}; } } catch { # Handles exceptions warn 'Something really bad happened: ' . $_; }; __END__

    See HTTP::Tiny.

    I'm not sure that you wanted the response content in your response string; if you really want the entire response you can get it with $res->as_string but you'd have to use a different HTTP library (e.g. LWP::Simple).

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Help me convert this to Perl
by FreeBeerReekingMonk (Deacon) on Nov 27, 2017 at 19:06 UTC
    Online Perl compiler I do not know, but you can perl2exe with PAR straight from your Strawberry Perl installation. (I'll assume you are on Windows Servers, where Security Policies apply).

    Now, this executable will be unsigned, and refuse to run (without a windows popup where you have to manually click). One of the workarounds is to Allow an Unsigned Application to Run As Privileged, but as a C# dev you should know those tricks.

Re: Help me convert this to Perl
by Anonymous Monk on Nov 27, 2017 at 19:36 UTC
    ignore sundialsvc4, he has literally no idea what he is talking about. He knows two things, Jack and shit. Worst posts of the day, week, month and year: see 'worst nodes' to your right.
A reply falls below the community's threshold of quality. You may see it by logging in.