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

I need to make 2 request, the first one with X-Requested-With header and the second without the header. How to delete it?
use LWP::UserAgent; my $ua = new LWP::UserAgent; $ua->default_header('X-Requested-With' => 'XMLHttpRequest'); my $response = $ua->get('http://site.com'); # delete header 'X-Requested-With my $response = $ua->get('http://site.com');

Replies are listed 'Best First'.
Re: Remove lwp header
by Corion (Patriarch) on May 19, 2013 at 23:04 UTC

    Have you looked at the ->default_headers() method? It returns the HTTP::Headers object that is used to set the default headers. I would look at removing the header from there, or not setting it as default in the first place.

Re: Remove lwp header
by Anonymous Monk on May 19, 2013 at 23:38 UTC
    See documentation for LWP::UserAgent get method
    use LWP::Simple qw/ $ua /; $ua->timeout(0.1); $ua->add_handler("request_send", sub { shift->dump; return }); $ua->get( 'http://localhost' ); $ua->get( 'http://localhost', 'X-Requested-With' => 'XMLHttpRequest'); __END__ GET http://localhost User-Agent: LWP::Simple/6.00 libwww-perl/6.05 (no content) GET http://localhost User-Agent: LWP::Simple/6.00 libwww-perl/6.05 X-Requested-With: XMLHttpRequest (no content)

    See also LWP::Debug

Re: Remove lwp header
by vsespb (Chaplain) on May 20, 2013 at 09:25 UTC
    Also, you can use
    $ua->request($my_request);
    instead of
    $ua->get('http://site.com');
    Then you can construct $my_request using http://search.cpan.org/perldoc?HTTP%3A%3ARequest and http://search.cpan.org/perldoc?HTTP%3A%3AMessage

    HTTP::Request is inherited from HTTP::Message and HTTP::Message does have a method remove_header