Hi,

I am currently workin on some JSON data requests from a webserver and I have trouble with the error handling.

It works fine with the correct credentials in authorization_basic. Now I tested it with wrong credentials and

I do not undestand why the scripts dies with "Can't locate LWP/Authen/Session.pm".

Here my code:

my $par_user = "123"; my $par_pass = "topsecret"; LOG_MSG($par_log_fh,$par_vbse,"n",$par_lglvl,5,"RESTREQUEST","REST + HTTP request start"); my $ua = LWP::UserAgent->new; $ua->agent("Mozilla/8.0"); # pretend we are very capable browser $ua->timeout(30); $ua->env_proxy; my $req = HTTP::Request->new; $req->header('content-type' => 'application/json'); $req->header('accept' => 'application/json'); $req->method($par_httpmethod); $req->authorization_basic($par_user,$par_pass); $req->uri($url_string); LOG_MSG($par_log_fh,$par_vbse,"n",$par_lglvl,5,"RESTREQUEST","Meth +od: $par_httpmethod"); LOG_MSG($par_log_fh,$par_vbse,"v",$par_lglvl,7,"RESTREQUEST","Auth +orisation: $par_user,$par_pass"); LOG_MSG($par_log_fh,$par_vbse,"n",$par_lglvl,5,"RESTREQUEST","URL: + $url_string"); my $response = $ua->request($req); LOG_MSG($par_log_fh,$par_vbse,"n",$par_lglvl,5,"RESTREQUEST","ssss +"); if ($response->is_success) { $http_status_code = $response->code; $http_decoded = $response->decoded_content; LOG_MSG($par_log_fh,$par_vbse,"n",$par_lglvl,5,"RESTREQUEST"," +HTTP request successful"); LOG_MSG($par_log_fh,$par_vbse,"n",$par_lglvl,5,"RESTREQUEST"," +HTTP status code: $http_status_code"); LOG_MSG($par_log_fh,$par_vbse,"v",$par_lglvl,7,"RESTREQUEST"," +HTTP decoded: $http_decoded"); LOG_MSG($par_log_fh,$par_vbse,"n",$par_lglvl,5,"RESTREQUEST"," +REST HTTP request finished"); return ($http_status_code,$http_decoded); } else { $http_status_code = $response->code; $http_decoded = $response->decoded_content; LOG_MSG($par_log_fh,$par_vbse,"n",$par_lglvl,3,"RESTREQUEST"," +HTTP request NOT successful"); LOG_MSG($par_log_fh,$par_vbse,"n",$par_lglvl,3,"RESTREQUEST"," +HTTP status code: $http_status_code"); LOG_MSG($par_log_fh,$par_vbse,"n",$par_lglvl,3,"RESTREQUEST"," +HTTP decoded: $http_decoded"); LOG_MSG($par_log_fh,$par_vbse,"n",$par_lglvl,3,"RESTREQUEST"," +REST HTTP request finished"); return ($http_status_code,$http_decoded); }

Here the output (don't worry for my died filehandle on log module)

2016.11.15,14:23:36,NOTICE,RESTGETPORTS,REST get ports start 2016.11.15,14:23:36,NOTICE,RESTREQUEST,REST HTTP request start 2016.11.15,14:23:36,NOTICE,RESTREQUEST,Method: GET 2016.11.15,14:23:36,NOTICE,RESTREQUEST,URL: http://10.3.3.3/Configurat +ionManager/v1/objects/storages/800000058068/ports
2016.11.15,14:23:36,ERROR,GENERAL,Script died: Can't locate LWP/Authen +/Session.pm in @INC (@INC contains: G:/development/HiCHperfpkg/bin\.. +\lib\modules G:/development/HiCHperfpkg/bin C:/Perl64/site/li b C:/Perl64/lib .) at (eval 72) line 2.
print() on closed filehandle $fh at G:/development/HiCHperfpkg/bin\..\ +lib\modules/xxx_LOG.pm line 92. 2016.11.15,14:23:36,WARNING,GENERAL,Script warning: print() on closed +filehandle $fh at G:/development/HiCHperfpkg/bin\..\lib\modules/xxx_L +OG.pm line 92. 2016.11.15,14:23:36,NOTICE,RESTREQUEST,ssss print() on closed filehandle $fh at G:/development/HiCHperfpkg/bin\..\ +lib\modules/xxx_LOG.pm line 92. 2016.11.15,14:23:36,WARNING,GENERAL,Script warning: print() on closed +filehandle $fh at G:/development/HiCHperfpkg/bin\..\lib\modules/xxx_L +OG.pm line 92. 2016.11.15,14:23:36,ERROR,RESTREQUEST,HTTP request NOT successful print() on closed filehandle $fh at G:/development/HiCHperfpkg/bin\..\ +lib\modules/xxx_LOG.pm line 92. 2016.11.15,14:23:36,WARNING,GENERAL,Script warning: print() on closed +filehandle $fh at G:/development/HiCHperfpkg/bin\..\lib\modules/xxx_L +OG.pm line 92. 2016.11.15,14:23:36,ERROR,RESTREQUEST,HTTP status code: 401 print() on closed filehandle $fh at G:/development/HiCHperfpkg/bin\..\ +lib\modules/xxx_LOG.pm line 92. 2016.11.15,14:23:36,WARNING,GENERAL,Script warning: print() on closed +filehandle $fh at G:/development/HiCHperfpkg/bin\..\lib\modules/xxx_L +OG.pm line 92. 2016.11.15,14:23:36,ERROR,RESTREQUEST,HTTP decoded: { "errorSource" : "/ConfigurationManager/v1/objects/storages/800000058 +068/ports", "message" : "The Authorization HTTP header, which is required for au +thentication, is not specified.", "solution" : "Specify the necessary value for the Authorization head +er.", "messageId" : "KART30010-E"

For example if I test it with wrong URL I get an expected error message.

2016.11.15,14:38:28,NOTICE,RESTGETSTORAGE,REST get storage start 2016.11.15,14:38:28,NOTICE,RESTREQUEST,REST HTTP request start 2016.11.15,14:38:28,NOTICE,RESTREQUEST,Method: GET 2016.11.15,14:38:28,NOTICE,RESTREQUEST,URL: http://10.0.1.11/Configura +tionManager/v1/objects/storages/ 2016.11.15,14:38:49,NOTICE,RESTREQUEST,ssss 2016.11.15,14:38:49,ERROR,RESTREQUEST,HTTP request NOT successful 2016.11.15,14:38:49,ERROR,RESTREQUEST,HTTP status code: 500 2016.11.15,14:38:49,ERROR,RESTREQUEST,HTTP decoded: Can't connect to 1 +0.70.4.14:80 (10060) LWP::Protocol::http::Socket: connect: 10060 at C:/Perl64/lib/LWP/Proto +col/http.pm line 51. 2016.11.15,14:38:49,ERROR,RESTREQUEST,REST HTTP request finished

Thanks for any hints how I can do a proper error handling for wrong user authentication.

regards deMichi

In reply to HTTP::Request - authorization_basic by demichi

Title:
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, details, 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, summary, 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.