alex_reader
alex_reader

Reputation: 711

Perl HTTP request : POST fails while GET succeeds

When I try to submit a POST request with Perl, it often ends in a 301 redirect to the homepage. Here is the code :

use LWP::UserAgent;

$ua = LWP::UserAgent->new;

# This does not work
my $url = 'http://www.opensubtitles.org/en/search2';
my $req = HTTP::Request->new(POST => $url);
$req->content('MovieName=the+terminator+(1996)');


# Pass request to the user agent and get a response back
print $req->as_string."\n";;
my $res = $ua->request($req);
if (!$res->is_success) { 
  print $res->status_line, "\n"; 
}
else { 
  print "Success in posting search\n";
}

In order to make it work, I have to manually use Firefox, go to the url (!). Then the script works. However, using a GET request works flawlessly :

# This works
my $url = 'http://www.opensubtitles.org/en/search2?MovieName=the+terminator+(1996)';
my $req = HTTP::Request->new(GET => $url);

Why is that ?

Upvotes: 0

Views: 350

Answers (1)

Borodin
Borodin

Reputation: 126722

The site doesn't expect a POST to that URL, so it redirects you to back to the search page.

Firefox will use GET, not POST, if you just put the URL into the address line, that's why it works.

Upvotes: 2

Related Questions