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

Hey monks, What is the best method to declare HTTP::Request statement?
$request = HTTP::Request->new(GET => 'http://www.example.com/');
or
my $request = new HTTP::Request('GET', $variable_here);

Replies are listed 'Best First'.
Re: 400 error
by 1nickt (Canon) on Dec 01, 2018 at 02:55 UTC

    Hi, consider not building your own objects when the request is very simple (see HTTP::Tiny):

    my $url = 'http://www.example.com'; my $response = HTTP::Tiny->new->get($url); print "$response->{status} $response->{reason}\n"; print $response->{content} if length $response->{content};

    Hope this helps!


    The way forward always starts with a minimal test.
Re: 400 error
by Your Mother (Archbishop) on Nov 30, 2018 at 23:43 UTC

    Neither. :P

    use HTTP::Request::Common; my $req = GET "http://www.example.com/";
Re: 400 error
by Anonymous Monk on Nov 30, 2018 at 23:28 UTC
Re: 400 error
by futureman (Novice) on Dec 01, 2018 at 00:40 UTC
    <html><head><title>An Error Occurred</title></head> + <body><h1>An Error Occurred</h1><p>400 URL missing</p></body></html> https://www.cnn.com had response code of 400 at soft1.pl line 92.
    Thank you for replying, please suggest, is this program crash because of request method, I tried using different method for HTTP:Request. Or is it LWP::ua issue. It keeps giving me this error for past 24 hours.

      You’re doing something wrong. With your current snippets it’s probably impossible to guess what.

      perl -MLWP::UserAgent -E 'say "OK" if LWP::UserAgent->new->get("https: +//www.cnn.com")->is_success' OK

        Hi, you might like L.


        The way forward always starts with a minimal test.
Re: 400 error
by futureman (Novice) on Dec 01, 2018 at 22:41 UTC
    Hey guys, thanks for replying and giving me information. Please look at the code, the output says 400 url is missing. The complete code.
    #!/usr/local/bin/perl use LWP::Simple; use HTML::Parse; use HTML::Element; use URI::URL; use HTTP::Request; use HTTP::Tiny; use vars qw($opt_h $opt_i $opt_a); use Getopt::Std; #system "cls"; #print "\n\n"; getopts('hia:'); my $all = !($opt_i || $opt_a); # all=1 when no option is set if ($opt_h || $#ARGV==-1) { print_help( ); # print help text when -h +or no args exit(0); } sub print_help { print <<"HELP"; usage: $0 [-hia] [URL] -h help -i find img references only -a find hyper link references only Example: $0 -a https://www.url_here.com HELP } $html = get $ARGV[0]; # always use [0] $parsed_html = HTML::Parse::parse_html($html); while ($html = shift @ARGV) { my ($code, $type, $data) = get_html($html, $opt_i, $opt_a); if (not_good($code, $type)) { next; } if ($opt_i || $all) { print_images($data, $html); exit(0); } if ($opt_a || $all) { print_hyperlinks($data, $html); exit(0); } } sub get_html( ) { my($html, $want_image, $want_link) = @_; # Create a User Agent object my $ua = new LWP::UserAgent; $ua->agent("Mozilla/61.0.0 (Windows; U; Windows 8.1; de; rv:1.9.2.3) + Gecko/20100401 Firefox/61.0.0 "); # Ask the User Agent object to request a URL. # Results go into the response object (HTTP::Reponse). # use HTTP::Request::Common; # my $request = GET $html; # my $request = GET $ARGV[0]; my $url = $html; print "$response->{status} $response->{reason}\n"; print $response->{content} if length $response->{content}; my $request = new HTTP::Request('GET', $ARGV[0]); my $response = HTTP::Tiny->new->get($url); my $response = $ua->request($request); if ($response->is_success) { print $response->content; } else { print $response->error_as_HTML; } print "$response->{status} $response->{reason}\n"; print $response->{content} if length $response->{content}; } sub not_good { my ($code, $type) = @_; if ($code != RC_OK) { warn("$html had response code of $code"); return 1; } if ($type !~ m@text/html@) { warn("$html is not HTML."); return 1; } return 0; } sub print_images { my ($data, $model) = @_; my $parsed_html=HTML::Parse::parse_html($data); for (@{ $parsed_html->extract_links(qw (body img)) }) { my ($link) = @$_; my ($absolute_link) = globalize_url($link, $model); print "$absolute_link\n"; } $parsed_html->delete( ); # manually do garbage collection } sub print_hyperlinks { my ($data, $model) = @_; my $parsed_html=HTML::Parse::parse_html($data); for (@{ $parsed_html->extract_links(qw (a)) }) { my ($link) = @$_; my ($absolute_link) = globalize_url($link, $model); print "$absolute_link\n"; } $parsed_html->delete( ); # manually do garbage collection } sub globalize_url( ) { my ($partial, $model) = @_; my $url = new URI::URL($partial, $model); my $globalized = $url->as_string; return $globalized; }

      Hi, I'll be glad to look at it after it's properly indented and formatted. (But you might find your error once you do that...) I can see already that there's a lot of what we call krufty kruft in there, i.e. leftovers from things you tried that you're now not using. For example you are loading HTTP::Tiny and LWP::Simple. You should clean that up too. Finally, you should shorten the program for posting here so it only has the code for the thing that breaks. The rest is just obscuring the issue, for us and for you. See SSCCE.

      Thanks!


      The way forward always starts with a minimal test.
        Thank you for guiding, this is the code which I think creating all sorts of issues. Please let me know if you think the code is perfect.
        my $ua = new LWP::UserAgent; $ua->agent("myagent"); my $url = $html; my $request = new HTTP::Request('GET', $ARGV[0]); my $response = HTTP::Tiny->new->get($url); my $response = $ua->request($request); if ($response->is_success) { print $response->content; } else { print $response->error_as_HTML; }
Re: 400 error
by futureman (Novice) on Dec 03, 2018 at 16:54 UTC
    Thank you all for guidelines and response, definitely I'll follow them.