I am trying to add a User-Agent
to the header in my HTTP request (in Ruby), but am unsure of the format of the user-agent string. here is how it is defined in Perl:
use LWP::UserAgent;
my $ua = new LWP::UserAgent(keep_alive=>1);
$ua->credentials($netloc, '', "$user", "$pwd");
If I try to print $ua;
, I get a hash which changes every time. I believe that in Ruby I should have the credentials embedded into a single string. How can I know how to format this string? Or, How can I successfully print the contents of $ua
in Perl?
Ruby code:
http = Net::HTTP.new("your.site.com", 80)
req = Net::HTTP::Get.new("/path/to/the/page.html", {'User-Agent' => '
your_agent_here'})
response = http.request(req)
Your Perl example does not show you setting the User-Agent header (using the agent
method). That's the header that identifies the software performing the transaction. You are adding Basic Auth credentials in the Perl example, which is how the user demonstrates they are allowed to access the resource. They are completely different things.
When you print $ua
, you are seeing the memory address of the object, like LWP::UserAgent=HASH(0x7fa9db00b1b0)
.
If you want to set the User-Agent string in the Ruby example, fill in the string you want.
If you want to supply the username and password for Basic Authorization, see the example in the Net::HTTP docs:
uri = URI('http://example.com/index.html?key=value')
req = Net::HTTP::Get.new(uri)
req.basic_auth 'user', 'pass'
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
puts res.body