Error code 302 from HTTP POST operation

I have a perl script that sends data to a web service that I wrote in php ...

This is the code:

    use LWP::UserAgent;

    my $ua = LWP::UserAgent->new;

    my $server_endpoint = "";

    my $req = HTTP::Request->new(POST => $server_endpoint);
    $req->header('content-type' => 'application/json');
    $req->header('x-auth-token' => 'kfksj48sdfj4jd9d');

    # add POST data to HTTP request body
    my $post_data = '{ "name": "Dan", "address": "NY" }';

    my $resp = $ua->request($req);
    if ($resp->is_success) {
         my $message = $resp->decoded_content;
        print "Received reply: $message\n";
    else {
        print "HTTP POST error code: ", $resp->code, "\n";
        print "HTTP POST error message: ", $resp->message, "\n";


When I submit a request, I get this response:

   HTTP POST error code: 302
   HTTP POST error message: Found



  • How can I get rid of this error or is it even a bug even though it says Found?
  • How can I get the return value of a message?
  • What is the correct way to post data? (The code above is copied from this site . My php site receives post data and echoes or just prints it as return.)

Thanks in advance.


source to share

1 answer

The 302 error from the server is a redirect command for the client. If you use the default configuration LWP::UserAgent

, it will automatically follow redirects up to a maximum of seven times. If you don't get a successful response, it means that either you have redirects disabled (which looks unlikely from the code you posted unless you provided some configuration details for it LWP::UserAgent

) or that you "re getting stuck in a redirect loop.

You can view the redirect data by inspecting the object HTTP::Response


my $resp = $ua->request($req);

# check for success, etc.

if ($resp->is_redirect) {
    # check the number of redirects that the script has made:
    say "n redirects: " . $resp->redirects;


With the default LWP :: UA settings, seven is the maximum number of redirects you will receive before LWP :: UA leaves.

More information about forwarding can be obtained by calling $resp->redirects

in the context of the array:

# @redirects is an array of HTTP::Response objects
my @redirects = $resp->redirects;

# print out the 'location' header for each Response object to track the redirection:
say "Location: " . $_->header('location') for @redirects;

# or, for more comprehensive troubleshooting, print out the whole response:
say "Response: " . $_->as_string for @redirects;


Sample output for a request to that redirects once:

# say "n redirects: " . $resp->redirects;
n redirects: 1

# say "Location: " . $_->header('location') for @redirects;

# say "Response: " . $_->as_string for @redirects;
Response: HTTP/1.1 302 Found
Cache-Control: private
Connection: close
Date: Fri, 10 Oct 2014 10:45:41 GMT
Server: GFE/2.0
Content-Length: 261
Content-Type: text/html; charset=UTF-8
Alternate-Protocol: 80:quic,p=0.01
Client-Date: Fri, 10 Oct 2014 10:45:39 GMT
Client-Response-Num: 1
Title: 302 Moved

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<H1>302 Moved</H1>
The document has moved
<A HREF=";ei=1bg3VJikJ_HH8gfOk4GwDw">here</A>.


I am assuming that you are stuck in a redirect loop and that is why you are not getting the expected response from your PHP script.

NB: to include say

other useful features from Perl 5.10 and later, put

use feature ':5.10';


at the top of your script after use strict; use warnings;




All Articles