in reply to Re^2: 400 error
in thread 400 error

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; }

Replies are listed 'Best First'.
Re^4: 400 error
by hippo (Archbishop) on Dec 03, 2018 at 10:16 UTC
    my $response = HTTP::Tiny->new->get($url); my $response = $ua->request($request);

    Unfortunately, the code isn't perfect. You've declared and assigned to $response twice on subsequent lines and therefore the effect of the former line is nil. If you had used warnings this would have been clear.

    This isn't an SSCCE because you are referring to $ARGV[0] without indication of what it might be and you have not used either of the modules. Here is an SSCCE for contrast.

    #!/usr/bin/env perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new (agent => 'myagent'); my $url = 'https://www.perlmonks.org/'; my $request = HTTP::Request->new ('GET', $url); my $response = $ua->request ($request); print $response->status_line . "\n";
Re^4: 400 error
by 1nickt (Canon) on Dec 03, 2018 at 12:38 UTC

    Hi, as hippo pointed out, this is not an SSCCE, and you are declaring $response twice. Additionally:

    • To be clear, you do *not* need to create a request object manually if you use HTTP::Tiny (nor LWP::UserAgent for that matter)
    • While we don't know how you have stored a URL in $html, it's likely not a good name for a variable that does not contain HTML.
    • You probably don't want to print the error as HTML if you are not returning output to a browser
    You need to stop throwing in snippets from various sources and settle on one tool, and take the time to learn it. That involves spending time with the documentation. The example I originally showed was copied almost verbatim from the HTTP::Tiny documentation. I suggest that you start with the SYNOPSIS of the module that you choose, and once you have that running in your environment, you can make changes to it, one line at a time, so you can see immediately if you made a mistake.

    This is an SSCCE that should be copied into a file (eg 'foo.pl') and run with $ perl foo.pl http://example.com

    use strict; use warnings; use HTTP::Tiny; my $url = shift; my $response = HTTP::Tiny->new->get($url); if ( $response->{success} ) { print "OK: $response->{content}\n"; } else { print "Failed: $response->{status} $response->{reason}\n"; } __END__
    ... or this ...
    use strict; use warnings; use LWP::UserAgent; my $ua = new LWP::UserAgent; # possibly add advanced user agent options here my $url = shift; # possibly construct a request object here my $response = $ua->get($url); # if not using a request object if ($response->is_success) { print $response->decoded_content, "\n"; } else { die $response->status_line; } __END__

    Hope this helps! There are no shortcuts to learning, not if you want to understand.


    The way forward always starts with a minimal test.