demichi has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTTP::Request - authorization_basic
by davido (Cardinal) on Nov 15, 2016 at 16:14 UTC | |
by demichi (Beadle) on Nov 16, 2016 at 09:31 UTC | |
|
Re: HTTP::Request - authorization_basic
by hippo (Archbishop) on Nov 16, 2016 at 11:19 UTC | |
by demichi (Beadle) on Nov 16, 2016 at 12:03 UTC | |
by Corion (Patriarch) on Nov 16, 2016 at 12:12 UTC |