user316389
user316389

Reputation: 87

Unable to pass custom header using perl module HTTP::Request::Generator

I'm using atom and testing out HTTP::Request::Generator PERL module. Code , below works on most part but I'm unable to send cookies or headers, it only displays default headers even when I have set in my code.

use strict;
use warnings;
use HTTP::Request::Generator 'generate_requests';
use LWP::UserAgent;

my $ua = 'LWP::UserAgent'->new;

my $gen = generate_requests(
    method  => 'GET',
    host    => [  'https://abc.ai/' ],
    pattern => 'https://abc.ai',

    headers => {
        "User-Agent" => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64',
        "Cookie" => '_abc',
    },
    wrap    => sub {
        my ( $req ) = @_;
        # Fix up some values
        $req->{'headers'}{'Content-Length'} = 666;
    },
    wrap => \&HTTP::Request::Generator::as_http_request,
);

while ( my $req = $gen->() ) {
    my $response = $ua->request( $req );
    # print $response->protocol, ' ', $response->status_line, "\n";
    print $req->headers->as_string, "\n";
    print $req->as_string();
    # Do something with $response here?

    if ($response->is_success) {
        #  print $response->decoded_content;
        print $response ->header('title');
    }
    else {
        die $response->status_line;
    }
}

Output User-Agent: libwww-perl/6.31 Login

The title page indicate me I'm not logged in this cookie is fine and i have tested it using curl i can manually login and retrieve required resource. Why its failing for perl, how can access my header options in code above. Thanks.

Solution

body_params => {
       comment => ['Some comment', 'Another comment, A++'],
   },

Got it solved adding above code.

Upvotes: 2

Views: 286

Answers (1)

tobyink
tobyink

Reputation: 13674

You can't provide the same option (wrap) twice:

    wrap    => sub {
        my ( $req ) = @_;
        # Fix up some values
        $req->{'headers'}{'Content-Length'} = 666;
    },
    wrap => \&HTTP::Request::Generator::as_http_request,

This may work though:

    wrap    => sub {
        my ( $req ) = @_;
        # Fix up some values
        $req->{'headers'}{'Content-Length'} = 666;
        
        return HTTP::Request::Generator::as_http_request( $req );
    },

Also the headers option appears to take an arrayref of hashrefs, like this:

    headers => [
        {
            "User-Agent" => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
            "Cookie" => '_abc',
        },
    ],

I guess the reason for that is so you can provide alternative sets of headers:

    headers => [
        {
            "User-Agent" => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
            "Cookie" => '_abc',
        },
        {
            "User-Agent" => 'Mozilla/1.0 (Hoover Vacuum Cleaner)',
            "Cookie" => '_def',
        },
    ],

That way your request generator can generate two requests for each page, using different User-Agent strings, or different cookies (so logged in as different users), or different Accept headers, or whatever.

Upvotes: 2

Related Questions