How to set custom headers in LWP::UserAgent requests in Perl?
Question
How to set custom headers in LWP::UserAgent requests in Perl?
Setting Custom Headers with LWP::UserAgent in Perl
When working with HTTP requests in Perl, the LWP::UserAgent module is a powerful and flexible choice. Sometimes, you need to add custom headers to your requests—for example, to specify an API key, user agent string, or other HTTP headers required by the server.
Here’s how you can set custom HTTP headers with LWP::UserAgent:
- Create an
LWP::UserAgentobject. - Create an
HTTP::Requestobject for the HTTP method & URL you want. - Use the
headermethod on the request object to set custom headers. - Call
requeston the user agent with your modified request object.
Important Perl Concepts
Note that LWP::UserAgent and HTTP::Request come from separate classes, so you build the request first and then hand it to the user agent to perform. Headers are set as key-value pairs in the HTTP::Request object.
Perl’s flexible context and sigils mean you can easily manipulate and pass these objects around. Unlike some other HTTP clients, LWP::UserAgent doesn’t require TLS configuration for simple use, but always consider HTTPS support in real apps.
Runnable Example
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
# Create a user agent
my $ua = LWP::UserAgent->new;
# Target URL (httpbin echoes back request data - great for testing)
my $url = 'https://httpbin.org/get';
# Create an HTTP GET request
my $req = HTTP::Request->new(GET => $url);
# Set custom headers
$req->header('X-Custom-Header' => 'MyValue');
$req->header('User-Agent' => 'MyLWPClient/1.0');
$req->header('Authorization' => 'Bearer ABC123XYZ');
# Perform the request
my $res = $ua->request($req);
# Check and print response
if ($res->is_success) {
print "Response (first 500 chars):\n";
print substr($res->decoded_content, 0, 500), "\n";
} else {
print "HTTP Request failed: ", $res->status_line, "\n";
}
What This Code Does
- Initializes a new
LWP::UserAgentobject. - Makes a GET request with three custom headers:
X-Custom-Header,User-Agent, andAuthorization. - Sends the request, then prints either the first 500 characters of the response body or an error message.
Key Points & Common Pitfalls
- Headers must be set on the HTTP::Request object, not directly on
LWP::UserAgent. - Be mindful of header names: header names are case-insensitive, but the standard style capitalizes each word and uses hyphens.
User-Agentcan be overridden as a header this way; otherwise,LWP::UserAgentuses a default string.- When setting
Authorization, ensure your credentials follow the protocol (e.g.,Basic,Bearer). This example uses a Bearer token format as a mock. - For POST or PUT requests, you can add content (body) with
$req->contentor$req->content(\$scalar). - Perl’s flexibility allows multiple calls to
$req->header()to set many headers conveniently.
Version Notes
LWP::UserAgent and HTTP::Request use standard interfaces that have been stable across Perl 5.10+ releases. No special version handling is needed here. This approach works well on all modern Perl installations.
In conclusion, setting custom HTTP headers in LWP::UserAgent requires creating an HTTP::Request object, adding headers to it with the header method, and sending the request through your user agent. This pattern supports TMTOWTDI ("There’s more than one way to do it") by allowing full control over HTTP request details.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 99ms
HTTP Request failed: 500 Can't verify SSL peers without knowing which Certificate Authorities to trust
(empty)Was this helpful?
Related Questions
- How to handle gzip compressed responses with LWP in Perl?
- How to use LWP::Simple for quick GET requests in Perl?
- How to parse HTML response with HTML::TreeBuilder in Perl?
- How to use Mojo::UserAgent for HTTP requests in Perl?
- How to set connection keep-alive with LWP::UserAgent in Perl?
- How to use LWP::Protocol::https for HTTPS requests in Perl?