Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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!

In reply to How to pass credentials through REST::Client by thanos1983

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2021-01-27 23:29 GMT
    Find Nodes?
      Voting Booth?