简体   繁体   English

无法使用 perl 模块 HTTP::Request::Generator 传递自定义 header

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

I'm using atom and testing out HTTP::Request::Generator PERL module.我正在使用atom并测试HTTP::Request::Generator PERL 模块。 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.下面的代码在大部分情况下都有效,但我无法发送 cookies 或标头,即使我在代码中进行了设置,它也只显示默认标头。

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 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. title页表明我没有登录此 cookie 很好,我已经使用curl对其进行了测试,我可以手动登录并检索所需的资源。 Why its failing for perl, how can access my header options in code above.为什么 perl 失败,如何在上面的代码中访问我的 header 选项。 Thanks.谢谢。

Solution解决方案

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

Got it solved adding above code.添加上面的代码就解决了。

You can't provide the same option ( wrap ) twice:您不能两次提供相同的选项 ( wrap ):

    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选项似乎采用 hashrefs 的 arrayref,如下所示:

    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.这样,您的请求生成器可以为每个页面生成两个请求,使用不同User-Agent字符串,或不同的 cookies(因此以不同的用户身份登录),或不同的Accept标头,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM