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

Hello fellow Monks,

I am working on a small project trying to learn more about REST Web Service. I have completed my back end and I am working on my front end. I was thinking that I should use REST::Client and I have created a small sample of code that works as expected so far.

Sample of code (

package ClientRest; use Carp; use strict; use warnings; use Data::Dumper; use version; our $VERSION; $VERSION = qv('0.0.1'); use JSON; use REST::Client; use constant { PORT_MIN => 1, PORT_MAX => 65535, }; sub new { my $class = shift; my $self = { _host => shift, }; _parameterValidation($self); # instatiate Rest::Client and create constructor my $client = REST::Client->new({ host => $self->{_host}, timeout => 10, }); $self->{_client} = $client; bless $self, $class; return $self; } sub _parameterValidation { my( $self ) = @_; croak "Invalid host syntax: sample 'http://<host>:<port>' " unless ( $self->{_host} =~ /^http:\/\/\d{1,3}\.\d{1,3}\.\d{1,3}\.\ +d{1,3}\:\d{1,5}$/ ); } sub getSnippets { my ($self) = @_; # Simple GET request for testing purposes $self->{_client}->GET('/snippets/'); if (index($self->{_client}->responseContent(), "Can\'t connect to" +) != -1) { my @array = split /[http:\/\/]/, $self->{_host}; @array = grep { $_ ne '' } @array; croak "Server is unavailable at IP: " .$array[0]." and Port: " .$array[1]; } return decode_json $self->{_client}->responseContent(); } # Module further implementation here 1;

Sample of code (

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use MyClientRest::ClientRest; my $host = ""; # instatiate class my $object = new ClientRest( $host ); my $data = $object->getSnippets(); print Dumper $data; __END__ $ perl $VAR1 = { 'count' => 2, 'previous' => undef, 'next' => undef, 'results' => [ { 'highlight' => ' +ets/1/highlight/', 'language' => 'perl', 'id' => 1, 'url' => ' +', 'style' => 'emacs', 'code' => 'print "Test POST Request"', 'owner' => 'user', 'title' => 'Default Title', 'linenos' => bless( do{\(my $o = 0)}, 'JSON +::PP::Boolean' ) }, { 'code' => 'print "Test POST Request"', 'owner' => 'user', 'title' => 'Test Title', 'linenos' => $VAR1->{'results'}[0]{'linenos +'}, 'highlight' => ' +ets/2/highlight/', 'language' => 'perl', 'id' => 2, 'url' => ' +', 'style' => 'emacs' } ] };

My problem is that through the documentation I do not see how I can send the username and password that is necessary in some transaction(s). For example I use curl to POST some data:

$ curl -i -X POST -H "Content-Type:application/json" -u user:password +http://localhost:8000/snippets/ -d '{ > "title": "Test Title", > "code": "print \"Test POST Request\"", > "linenos": false, > "language": "perl", > "style": "emacs" > }' HTTP/1.0 201 Created Date: Fri, 06 Apr 2018 09:14:54 GMT Server: WSGIServer/0.1 Python/2.7.12 Vary: Accept, Cookie X-Frame-Options: SAMEORIGIN Content-Type: application/json Content-Length: 252 Allow: GET, POST, HEAD, OPTIONS {"url":"http://localhost:8000/snippets/2/","id":2,"highlight":"http:// +localhost:8000/snippets/20/highlight/","owner":"user","title":"Test T +itle","code":"print \"Test POST Request\"","linenos":false,"language" +:"perl","style":"emacs"}

I have found this simple tutorial Writing a REST client in Perl which shows that I can pass the username and password through the header.

Sample of code:

my $headers = {Accept => 'application/json', Authorization => 'Basic ' + . encode_base64($username . ':' . $password)};

So I am wondering if there is an alternative solution on how to pass the credentials through the module. I would expect something like a dictionary (pseudo code) user => 'user', pass => 'pass'. Any other recommendations of alternative modules?

Thanks in advance for your time and effort.

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

Replies are listed 'Best First'.
Re: How to pass credentials through REST::Client
by Anonymous Monk on Apr 06, 2018 at 10:06 UTC

      Hello Anonymous Monk,

      Thanks a lot that is exactly what I was looking for.

      BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: How to pass credentials through REST::Client
by jimpudar (Pilgrim) on Apr 07, 2018 at 03:57 UTC

    Hi Thanos,

    I know your question was already answered, but I thought I would suggest you take a look at

    The entire Mojolicious project is excellent and worth taking a look.

    It's very easy to create REST services with Mojolicious, but the UserAgent is also fantastic and a pleasure to work with.

    For example, here are a couple of requests with different auth types. These examples are from the page I linked.

    # Quick JSON API request with Basic authentication my $value = $ua->get('')->result +->json; # JSON POST (application/json) with TLS certificate authentication my $tx = $ua->cert('tls.crt')->key('tls.key') ->post('' => json => {top => 'secret'});