Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Sample REST request & response code

by kanewilliam7777 (Novice)
on Sep 06, 2018 at 06:23 UTC ( [id://1221830]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all,

I have to implement REST API conecpt in PERL

So I need sample localhost request & response code using REST concept.

Please anybody have sample code.

Replies are listed 'Best First'.
Re: Sample REST request & response code
by marto (Cardinal) on Sep 06, 2018 at 06:41 UTC
Re: Sample REST request & response code
by 1nickt (Canon) on Sep 06, 2018 at 11:41 UTC

    Hi, there's sample code in the documentation of any decent CPAN module, in the SYNOPSIS as well as in the test files. Larger platforms such as web application servers typically offer expansive documentation. Which have you looked at so far?

    I recommend using Dancer2 to serve your REST API, and you might like REST::Client for making requests to it.

    Server:

    use Dancer2; get '/' => sub { "Hello World" }; dance;
    Client:
    use REST::Client; my $client = REST::Client->new(); $client->GET('http://localhost:3000/'); print $client->responseContent();

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Sample REST request & response code
by thanos1983 (Parson) on Sep 06, 2018 at 08:40 UTC

    Hello kanewilliam7777,

    Some time ago (not more than 5-6 months) I wrote a small module for doing (GET, POST, DELETE, PUT and POST file). It should all what you need when you modify it to your requirements.

    See the repo in GIT thanos1983/Perl5-MyClientRest-ClientRest.

    Sample of how to call the module:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use MyClientRest::ClientRest; my $host = "http://127.0.0.1:8000"; # instatiate class my $object = new ClientRest( $host ); my $username = "user"; my $password = "password"; my $url = "/snippets/"; my $snippets = $object->getSnippets( $url, $username, $password ); # print Dumper $snippets; my $hashRef = { "title" => "Test Title", "code" => "print \"Test PUT Thanos Request\"", "linenos" => "false", "language" => "perl", "character" => "\x{00AE}", "style" => "emacs" }; my %options = ( "url" => $url, "hashRef" => $hashRef, "username" => $username, "password" => $password ); # my $post = $object->postSnippets( %options ); # print Dumper $post; my %optionsUpdate = ( "url" => $url . '49/', "hashRef" => $hashRef, "username" => $username, "password" => $password ); my $put = $object->putSnippets( %optionsUpdate ); print Dumper $put;

    You need to configure it to your needs but it should be more than a good starting point. The module is very simple so it should be straight forward for modifications.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      hi thanos I'm interested in replicating this. Could $host be a remote website with which I currently use sftp?

      #!/usr/bin/perl -w use 5.011; use Net::SFTP::Foreign; my $upload_file = shift; my %vars = ( server_dir => 'perlmonks/scripts/', image_dir => 'pmimage', ); my $rvars = \%vars; say Dumper $rvars; my $sftp = get_tiny(); my $server_dir = $vars{"server_dir"}; $sftp->mkdir("/$server_dir") or warn "mkdir1 failed $!\n"; $sftp->setcwd("/$server_dir") or warn "setcwd1 failed $!\n"; $sftp->put($upload_file) or warn "upload put failed $!\n"; $sftp->ls($server_dir) or warn "can't ls $!\n"; say "return is $sftp"; undef $sftp; sub get_tiny { use 5.011; use warnings; use Net::SFTP::Foreign; use Config::Tiny; use Data::Dumper; my $ini_path = qw( /home/bob/Documents/html_template_data/3.values.i +ni ); say "ini path is $ini_path"; my $sub_hash = "my_sftp"; my $Config = Config::Tiny->new; $Config = Config::Tiny->read( $ini_path, 'utf8' ); say Dumper $Config; # -> is optional between brackets my $domain = $Config->{$sub_hash}{'domain'}; my $username = $Config->{$sub_hash}{'username'}; my $password = $Config->{$sub_hash}{'password'}; my $port = $Config->{$sub_hash}{'port'}; #dial up the server say "values are $domain $username $password $port"; my $sftp = Net::SFTP::Foreign->new( $domain, user => $username, port => $port, password => $password ) or die "Can't connect: $!\n"; return $sftp; } __END__

      This is what I've been using to try to start up a conversation with a remote server and it's either not the right thing or I don't know how to script it. I looked at your repo. Can you say a few words as to what a Snippet is? Thanks,

        Hello Aldebaran,

        The quick answer to your question Could $host be a remote website with which I currently use sftp? is yes it can.

        The longer version and my recommendation is not to use HTTP/HTTPS to upload files. My module is a customization which in the background uses REST::Client to do all the communication.

        Further analysis why I do not recommend to use HTTP/HTTPS for uploading files?

        HTTP and HTTPS are also stateless protocols. They may require new conn +ections for each transfer, and so they may not be suitable if a large + number of files need to be transferred.

        Reference Source HTTPS or SFTP – which is best?.

        SFTP is a protocol designed for this purposes (file transfer) using SSH connections.

        If you ask my opinion I would continue using the Net::SFTP::Foreign module but with a small modification, I would use SSH keys instead of sending username and password. If you want to see a sample of code on how to use SSH keys connection Re: Net::SFTP::Foreign Password Authentication Hangs (UPDATED). There are many reasons on why to use SSH keys instead of password, the really fast answer is because it is way more secure and less communications between client/server.

        To conclude, yes it is possible to send a file through HTTP/HTTPS (SSL encryption) with the help of my module that uses REST::Client or directly modify it for your needs, but I would not recommend it unless if you did not had the ability to use SSH strictly HTTP/HTTPS.

        You also asked what is Snippet is the name that I used for the data that is send from the server.

        Let me know if you have more questions.

        BR / Thanos

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Sample REST request & response code
by Anonymous Monk on Sep 06, 2018 at 06:35 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-19 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found