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

I can easily get a valid response when I execute this curl command:
curl -X GET --header 'Accept: application/json' 'https://ptabdata.uspt +o.gov/ptab-api/documents'
but if I try to get the same information using Perl I failed:
use LWP::UserAgent; my $url = 'https://ptabdata.uspto.gov/ptab-api'; my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0, SSL_verify_mode => 'SSL_VERIFY +_NONE' }, cookie_jar => {}, ); my $response = $ua->get( "$url/documents", 'Accept' => 'application/js +on' ); print $response->content;
here's the output when I execute the perl script:
Can't connect to ptabdata.uspto.gov:443 Connection reset by peer at /System/Library/Perl/Extras/5.18/LWP/Proto +col/http.pm line 51.
what am I doing wrong?

Replies are listed 'Best First'.
Re: Can't connect to https using LWP::UserAgent
by hippo (Archbishop) on Feb 08, 2017 at 08:31 UTC

    Your posted script works fine for me and I get apparently valid JSON in return.

    This is using

    • Perl 5.20.3
    • LWP::UserAgent 6.15
    • Mozilla::CA 20141217

    HTH, Hippo

Re: Can't connect to https using LWP::UserAgent
by madtoperl (Hermit) on Feb 08, 2017 at 07:10 UTC
    you can try below if you are not doing SSL validation,
    untested though since I do not have permission to install Net::SSL module in my box
    use strict; use warnings; use LWP::UserAgent; use HTTP::Request::Common qw(GET); use Net::SSL; my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0}, ); my $req = GET 'https://ptabdata.uspto.gov/ptab-api/documents'; my $res = $ua->request($req); if ($res->is_success) { print $res->content; }else{ print $res->status_line . "\n"; }
      Your solution works for me! Thank you! Do you know why this works when my initial attempt failed? (I'd like to know what I did wrong)
        The key was disabling the verify_hostname option to the https handler. You probably should install Mozilla::CA as per the LWP::Protocol::https documentation so that you can verify the certificates presented by the server.