Search code examples
perlwww-mechanize

Problems authenticating through API key sent in headers WWW::Mechanize


To get a response from an API, the key must be sent as a header along with the request. If I send it from the command line using curl -Ss -H "APIKEY: KEY" https://www.example.com/some/stuff the request succeeds. However, if I send it using

my $path = "https://www.example.com/some/stuff"
my $mech = WWW::Mechanize->new();
$mech->add_header('APIKEY'=>'KEY');
$mech->get($path);

I receive Error GETing https://www.example.com/some/stuff: Can't connect to www.example.com:443 (Permission denied). How can I properly supply this header so I authenticate?


Solution

  • I wrote a server side script that shows the output of headers from both examples and APIKEY was set identically in both cases. There were some differences in HTTP_ACCEPT / HTTP_ACCEPT_ENCODING and WWW::Mechanize adds some additional headers:

    'downgrade-1.0' => '1'
    'force-response-1.0' => '1'
    'nokeepalive' => '1'
    

    So I would suggest the problem is somewhere else.