How to Disable 302 Redirect Response and Force Automatic Redirection in cURL Requests


5 views

When testing redirects using cURL, you might encounter this common scenario:

$ curl "test.test.com/landing/0192"
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://redirect.redirect.com/landing?xid=0192&amp;tt=192">here</a>.</p>
<hr>
<address>Apache/2.2.17 (Fedora) Server at spencervm.adverplex.com Port 80</address>
</body></html>

The 302 status code indicates a temporary redirect. While browsers automatically follow these redirects, cURL by default shows the intermediate response. The final destination is actually:

http://redirect.redirect.com/landing?xid=0192&tt=192

To make cURL behave like a browser and automatically follow redirects, use the -L or --location flag:

$ curl -L "test.test.com/landing/0192"

This will output the final destination's content instead of the 302 response.

For more control over redirects, combine these options:

$ curl -L --max-redirs 5 --post301 --post302 "test.test.com/landing/0192"

Where:

  • --max-redirs 5: Limits redirects to prevent infinite loops
  • --post301/--post302: Preserves POST method during redirects

To see the complete redirect chain while still getting the final content:

$ curl -v -L "test.test.com/landing/0192"

The -v flag provides verbose output showing each step of the redirect process.

Here's how to handle redirects in PHP:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "test.test.com/landing/0192");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

In Python, redirects are handled automatically by the requests library:

import requests
response = requests.get("test.test.com/landing/0192", allow_redirects=True)
print(response.text)

When testing redirects with curl, you might encounter a situation where the server responds with a 302 Found status instead of automatically following the redirect. This is the default behavior of HTTP/1.1 where servers indicate temporary URL moves.

$ curl "test.test.com/landing/0192"

Produces:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://redirect.redirect.com/landing?xid=0192&amp;tt=192">here</a>.</p>
<hr>
<address>Apache/2.2.17 (Fedora) Server at spencervm.adverplex.com Port 80</address>
</body></html>

To make curl automatically follow redirects without showing the 302 response, use the -L or --location flag:

$ curl -L "test.test.com/landing/0192"

This will:

  • Follow up to 50 redirects by default
  • Show only the final destination content
  • Preserve all headers and POST data during redirection

For more precise control over redirect behavior:

# Limit maximum number of redirects
$ curl -L --max-redirs 3 "test.test.com/landing/0192"

# Include redirect URLs in output
$ curl -L -v "test.test.com/landing/0192"

# Disable redirects completely
$ curl --max-redirs 0 "test.test.com/landing/0192"

For POST requests that need to follow redirects:

$ curl -L -X POST --data "param1=value1" "test.test.com/landing/0192"

To handle cookies during redirects:

$ curl -L -b cookies.txt -c cookies.txt "test.test.com/landing/0192"

If you prefer HTTPie, redirects are followed automatically by default:

$ http GET test.test.com/landing/0192

To disable automatic redirects:

$ http --follow=no GET test.test.com/landing/0192