Discussion:
How do I add headers using WWW::Mechanize?
(too old to reply)
Phil Powell
2008-02-19 21:09:48 UTC
Permalink
Consider my code snippet:

<code>
use strict;
use warnings;
use HTTP::Cookies;
use WWW::Mechanize;
use LWP::Debug qw(+);

my $mech = WWW::Mechanize->new();
$mech->agent_alias('Windows IE 6');
$mech->cookie_jar(HTTP::Cookies->new(autosave => 1));
$mech->add_header('UID' => 'phil', 'cn' => 'CN', 'id' => '777');

my $response = $mech->get('https://www.example.com');
die "Error at https://www.example.com\n", $response->status_line, "\n
Aborting" unless $response->is_success;

$response = $mech->response;
for my $key ($response->header_field_names()) {
print "response[$key] = ", $response->header($key), "\n";
}
</code>

I am trying to add new headers into the HTTP headers and yet I do not
see 'UID' nor 'cn' nor 'id' whenever I list all of the returned
headers even though I added them. What might I be doing wrong, and
thus, how can I fix it so that these three addtitional fields are
added to the HTTP headers?

Thanks
Phil
Joost Diepenmaat
2008-02-19 21:14:29 UTC
Permalink
Phil Powell <***@gmail.com> writes:

You're adding header to the request, but you're printing response
headers. The server may or may not use those headers in the response,
but if it's a web server it probably won't.

By the way, those look like LDAP fields. What are you trying to do?
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Phil Powell
2008-02-20 14:17:37 UTC
Permalink
Post by Joost Diepenmaat
You're adding header to the request, but you're printing response
headers. The server may or may not use those headers in the response,
but if it's a web server it probably won't.
By the way, those look like LDAP fields. What are you trying to do?
Ultimately what I am trying to do is to go to a particular website,
say

1) https://www.example.com/main/index.html

But you cannot go to that site unless you are authenticated via

2) https://www.example.com/registration/login.html
3) https://www.example.com/registration/login_2.html
4) [Siteminder] https://www.example.com/login/login.fcc

If not authenticated when you get to 1) you are automatically
redirected to 2). You fill out a form with username and password to
get to 3) which consists of a form with hidden values of your username
and password which automatically submit to 4), which does the
authentication and then once authenticated your username is passed as
a header value all the way back to 1).
Post by Joost Diepenmaat
--
Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
Joost Diepenmaat
2008-02-20 14:24:17 UTC
Permalink
Post by Phil Powell
Ultimately what I am trying to do is to go to a particular website,
say
1) https://www.example.com/main/index.html
But you cannot go to that site unless you are authenticated via
2) https://www.example.com/registration/login.html
3) https://www.example.com/registration/login_2.html
4) [Siteminder] https://www.example.com/login/login.fcc
If not authenticated when you get to 1) you are automatically
redirected to 2). You fill out a form with username and password to
get to 3) which consists of a form with hidden values of your username
and password which automatically submit to 4), which does the
authentication and then once authenticated your username is passed as
a header value all the way back to 1).
Ok, but none of the headers in your original post are standard HTTP
headers, which means:

1. they'll probably be ignored

2. they appear to have nothing to do with the problem you posted above.

For the problem above, you should probably use the WWW::Mechanize form
filling and posting methods. If the page works with a standard browser
(without plugins or javscript) There's generally no need to mess with
the headers. Of course, the siteminder fcc thing may do *something*
non-standard, but if it does, don't expect any other web server / page
to act like it.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Phil Powell
2008-02-20 14:30:50 UTC
Permalink
Post by Joost Diepenmaat
Post by Phil Powell
Ultimately what I am trying to do is to go to a particular website,
say
1)https://www.example.com/main/index.html
But you cannot go to that site unless you are authenticated via
2)https://www.example.com/registration/login.html
3)https://www.example.com/registration/login_2.html
4) [Siteminder]https://www.example.com/login/login.fcc
If not authenticated when you get to 1) you are automatically
redirected to 2).  You fill out a form with username and password to
get to 3) which consists of a form with hidden values of your username
and password which automatically submit to 4), which does the
authentication and then once authenticated your username is passed as
a header value all the way back to 1).
Ok, but none of the headers in your original post are standard HTTP
1. they'll probably be ignored
2. they appear to have nothing to do with the problem you posted above.
For the problem above, you should probably use the WWW::Mechanize form
filling and posting methods. If the page works with a standard browser
(without plugins or javscript) There's generally no need to mess with
the headers. Of course, the siteminder fcc thing may do *something*
non-standard, but if it does, don't expect any other web server / page
to act like it.
Actually I am using the WWW::Mechanize form filling and posting
methods, you have to to get username and password to the
Siteminder .fcc page for authentication. There is Javascript all
throughout the pages as well, which I heard WWW::Mechanize cannot work
with and will subsequently ignore, which ultimately should not matter
as most of it is client-side authentication scripting anyway. All
that the Siteminder fcc page does is authenticate and then return the
username as part of the HTTP response headers, which, again, you don't
see when you list the headers.
Post by Joost Diepenmaat
--
Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/- Hide quoted text -
- Show quoted text -
Loading...