Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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 (ClientRest.pm):

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 (restClient.pl):

#!/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 $data = $object->getSnippets(); print Dumper $data; __END__ $ perl restClient.pl $VAR1 = { 'count' => 2, 'previous' => undef, 'next' => undef, 'results' => [ { 'highlight' => 'http://127.0.0.1:8000/snipp +ets/1/highlight/', 'language' => 'perl', 'id' => 1, 'url' => 'http://127.0.0.1:8000/snippets/1/ +', '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' => 'http://127.0.0.1:8000/snipp +ets/2/highlight/', 'language' => 'perl', 'id' => 2, 'url' => 'http://127.0.0.1:8000/snippets/2/ +', '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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2021-05-19 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (186 votes). Check out past polls.

    Notices?