Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

REST::Client 500 Error

by ty_sopw (Initiate)
on Sep 15, 2020 at 14:51 UTC ( #11121798=perlquestion: print w/replies, xml ) Need Help??

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

Hi- I'm trying to call a rest API using the Perl module REST::Client. The end point URL uses https protocol and when run my Perl script to connect and get a reponse from the API, i get 500 Can't connect to <api host domain>:443 (connect: Connection refused) error. When i use the exact end point URL via a browser, i get valid response. I looked up this error and found some suggestions around turning off host verification to get around this error, but, that didn't help either. Is there anything else that i can try to prevent this error?

#!/usr/bin/perl use REST::Client; #$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; #Intentionally commented this o +ut as i was trying multiple opions to turn off host verification MAIN: { my $client = REST::Client->new( timeout => 30, ssl_opts => { verify_hostname => 0 },); $client->GET(<end point url>); #assigning the API response to a variable my $response = $client->responseContent(); print $response."\n"; }

Replies are listed 'Best First'.
Re: REST::Client 500 Error
by davido (Cardinal) on Sep 15, 2020 at 17:09 UTC

    Instantiate your own LWP::UserAgent object with the fields you deem useful. I don't think that your ssl_opts field is getting passed through. Here's an incomplete example:

    my $ua = LWP::UserAgent->new( ssl_opts => {verify_hostname => 0}, ); my $client = REST::Client->new( timeout => 30, useragent => $ua, );

    This has two benefits. First, it allows you to configure the user agent however you like. But second, it provides you full access to the LWP::UserAgent object, so that you can probe and inspect the transaction in far greater detail than what is exposed natively by REST::Client.


    Dave

      I did the change, but, still get the same error 500 Can't connect to <domain>:443 (connect: Connection refused). The URL we are hitting is external; do we have to add their CA to verify server certificates,$client->setCa('/path/to/ca.file'); or add their SSL cert and provide it to REST::Client, cert => '/path/to/ssl.crt'

      #!/usr/bin/perl use strict; use warnings; use REST::Client; use LWP::UserAgent; MAIN: { my $ua = LWP::UserAgent->new( ssl_opts => {verify_hostname => 0}, ); my $client = REST::Client->new( timeout => 30, useragent => $ua,); $client->GET(<end point url>); #assigning the API response to a variable my $response = $client->responseContent(); print $response."\n"; }
Re: REST::Client 500 Error
by NetWallah (Canon) on Sep 15, 2020 at 17:02 UTC
    Another possibility is that there may be HTTP redirection at your <end point url>.

    A browser would follow that to get your data - your REST client - will likely NOT do that.
    Try getting the URL with a verbose 'curl' command.

                    "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

Re: REST::Client 500 Error
by hippo (Chancellor) on Sep 15, 2020 at 22:31 UTC
    Is there anything else that i can try to prevent this error?

    Here are just a few of the things you could try. Note that since you've decided not to say what the exact URL involved is, you won't reap the most benefit of the collective experience of the Monastery. Why have you not provided this very important piece of data?

    1. Try setting appropriate headers.
    2. Make sure you are running the most recent versions of all the relevant modules, particularly Mozilla::CA and IO::Socket::SSL but also LWP::Protocol::https and REST::Client itself of course. Also ensure your version of openssl is up to date.
    3. Use strict and warnings.
    4. Don't even think about not validating the hostname or server certificate. It is 2020 and any remote service with an invalid certificate is not to be trusted.
    5. Try setting a known user agent identifier.
    6. Use 'debug4' with IO::Socket::SSL to trace the TLS connection.
    7. Tell us which version of Perl you are running, on which O/S and, as already mentioned, which precise URL.

    🦛

Re: REST::Client 500 Error
by Anonymous Monk on Sep 15, 2020 at 16:15 UTC
    Look on server side logs to see why it is refusing the connection.
Re: REST::Client 500 Error
by perlfan (Priest) on Sep 15, 2020 at 18:56 UTC
    I am not sure what the point of this module is, you can use HTTP::Tiny in your code - curl for super easy debugging. Also, you don't need the :443 to the URL, using https in the URI is sufficient for the default. Can you connect via curl or browser? 5xx indicates server error.

      I'm able to connect via browser and i do not explicitly use 443 in the URL. It's returned as part of the error my Perl program is throwing. So, if we use HTTP::Tiny, i understand it doesn't verify server identity by default; so, i assume i don't have to install any SSL certs. Is that correct?

        No, it doesn't verify ssl by default. HTTP::Tiny does require IO::Socket::SSL for connecting over SSL, however. Your Perl program is not throwing the 500, it is throwing an error because the server you're connecting to is responding with a 500 error. It's the server itself that is generating the 500.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11121798]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2020-09-20 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (120 votes). Check out past polls.

    Notices?