http://www.perlmonks.org?node_id=1039291

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

Hi,

I am working with REST::Client. I just want to login to a REST interface and get the authentication token.

Here is my curl command:

curl -i -k -H "Accept:application/*+xml;version=1.5" -u "username@syst +em:password" -X post https://something/api/sessions

And here is the curl output:

HTTP/1.1 200 OK Date: Mon, 17 Jun 2013 08:19:59 GMT x-authorization: qQHvPTvOa49l8EyuWSwmoDzMn8nVHWDbFUP+tC9RyMk= Set-Cookie: token=qQHvPTvOa49l8EyuWSwmoDzMn8nVHWDbFUP+tC9RyMk=; Secure +; Path=/ Content-Type: application/session+xml;version=1.5 Date: Mon, 17 Jun 2013 08:20:00 GMT Content-Length: 1055

I have removed my company specific info from above output. Anyway, the command works fine which means I have the information that is needed to get the authentication token.

I am trying to achieve the same via perl code.

Here is my code:

#!usr/bin/perl -w use strict; use REST::Client; use MIME::Base64; $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; ### to ignore the SSL my $username = 'username@system'; my $password = 'password'; my $url = "https://something/api/sessions"; my $client = REST::Client->new(); my $headers = { "Accept" => "application/*+xml;version=1.5", "Authorization" => 'Basic ' . encode_base64($username . + ':' . $password), }; $client->POST($url,$headers); print $client->responseContent();

Now my script does not print anything. It exits with nothing being printed on the screen. Also, if I change the password to the wrong one, it still exits without printing anything. I suspect the “POST” line in my code is not right but I cannot correct it. I tried various different combination of POST statement but none worked.

Could you tell me what am I missing here? It should at least print some error message when I provide wrong password but it is not doing so either.

Thanks.

Replies are listed 'Best First'.
Re: REST::Client does not provide any output (debugging x-authorization)
by Anonymous Monk on Jun 17, 2013 at 08:53 UTC

    Now my script does not print anything. It exits with nothing being printed on the screen.

    Turn on debugging so that you can examine the request that gets sent

    FWIW, I don't know what is x-Authorization , but its not like the thing you appear to be trying, its not like basic, this is basic

    use HTTP::Request ; my $r = HTTP::Request->new('http://localhost'); $r->authorization_basic(qw/ user pass /); print $r->as_string; __END__ http://localhost - Authorization: Basic dXNlcjpwYXNz

      Here is round trip using your data , note how it doesn't match :)

      #!/usr/bin/perl -- use Data::Dump; use HTTP::Request ; my $r = HTTP::Request->new('http://localhost'); $r->authorization_basic(qw/ username@system password /); dd( $r->as_string ); dd( $r = HTTP::Request->parse( $r->as_string ) ); dd( $r->authorization_basic ); __END__ "http://localhost -\nAuthorization: Basic dXNlcm5hbWVAc3lzdGVtOnBhc3N3 +b3Jk\n\n" bless({ _content => "", _headers => bless({ authorization => "Basic dXNlcm5hbWVAc3lzdGVtOnBh +c3N3b3Jk" }, "HTTP::Headers"), _method => "http://localhost", _uri => bless(do{\(my $o = "-")}, "URI::_generic"), }, "HTTP::Request") ("username\@system", "password")
      The actual is "x-appname-Authorization". I removed the appname from the string as it is our application code name which is still under development. Thanks for your time.

        The actual is "x-appname-Authorization". I removed the appname from the string as it is our application code name which is still under development. Thanks for your time.

        Ok, but that still doesn't explain what protocol what rfc that x-authorization header is supposed to follow

        X- means non-standard , application specific , it doesn't explain what application, what protocol, what format

        I don't know how curl determines this, but I imagine if you turned on more debugging/verbosity in curl you might find out