My code follows. Basically, I'm having a problem getting LWP to redirect after submitting a POST. A simple example is shown below.
When the Catherine Wheel search is used, Amazon determines that there's only one search result, and so it does a 302 Location: redirect to that page. Unfortunately, LWP isn't following this GET after the initial POST.
This is NOT trying to get LWP to follow a POST redirect.
I'm drawing a blank on this now and google is a blur. Any ideas? Thanks in advance,
Y
#!/usr/bin/perl
use LWP::UserAgent;
# $artist = "Pearl Jam"; # These two entries work because
+the repsonse from
# $album = "Ten"; # the post is a standard HTML pag
+e
$artist = "Catherine Wheel"; # These two entries do not work b
+ecause the response
$album = "Chrome"; # from the post is a 302 redirect
+ and its not being
# followed. It's not a POST redi
+rect, its a post which
# triggers a GET redirect.
$ua = new LWP::UserAgent;
$ua -> agent("YoYoMa/".$ua -> agent);
push @{ $ua->requests_redirectable }, 'POST'; # This line do
+esn't seem to make a difference. It fails with/without it.
$url = "http://www.amazon.com/exec/obidos/search-handle-form/ref=s
+_sf_pm/002-2036334-1552026";
$req = new HTTP::Request('POST',$url);
$req->content_type('application/x-www-form-urlencoded');
$req->content("page=1&index=music&field-artist=".$artist."&field-t
+itle=".$album."\n");
$res = $ua->request($req);
if ($res->is_success) {
print $res->content;
} else {
print "Redirect not followed.";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.