Skip to content

307 Temporary Redirect

Description

The 307 Temporary Redirect status code indicates that the requested resource has been temporarily moved to another URI, and future requests should still use the original URI. Unlike the 302 Found status code, the 307 Temporary Redirect explicitly preserves the HTTP method used in the original request when following the redirect.

This status code was introduced in HTTP/1.1 to address the inconsistent behavior of 302 Found across different browsers and clients. Some clients would change a POST request to a GET request when following a 302 Found redirect, which could lead to unexpected behavior.

The 307 Temporary Redirect ensures that if the original request was a POST, the redirected request will also be a POST, preserving the HTTP method semantics. This is particularly important for forms and API endpoints that rely on specific HTTP methods.

Syntax

The server responds with a 307 Temporary Redirect status and includes the temporary location:

HTTP/1.1 307 Temporary Redirect
Location: https://example.com/temporary-path
Content-Type: text/html
Content-Length: [length in bytes]

[Optional response body with a hyperlink to the temporary location]

Examples

Form Submission Example

A client submits a form that needs to be processed by a different server temporarily:

Client Request:

POST /submit-form HTTP/1.1
Host: forms.example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 54

name=John+Doe&email=john.doe%40example.com&age=30

Server Response:

HTTP/1.1 307 Temporary Redirect
Location: https://backup-server.example.com/submit-form
Content-Type: text/html
Content-Length: 235

<!DOCTYPE html>
<html>
<head>
  <title>Temporary Redirect</title>
</head>
<body>
  <p>This form is being temporarily processed by our backup server. Your browser will automatically resubmit your form data to <a href="https://backup-server.example.com/submit-form">the backup server</a>.</p>
</body>
</html>

The client should resubmit the POST request with the same form data to the URL specified in the Location header.

API Request Example

A client sends a PUT request to update a resource that is temporarily handled by a different endpoint:

Client Request:

PUT /api/users/123 HTTP/1.1
Host: api.example.com
Content-Type: application/json
Content-Length: 45
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

{
  "name": "John Smith",
  "email": "[email protected]"
}

Server Response:

HTTP/1.1 307 Temporary Redirect
Location: https://api-backup.example.com/api/users/123
Content-Type: text/plain
Content-Length: 102

This API endpoint is temporarily being served from our backup system. Your request will be redirected.

Load Balancing Example

A client sends a request during a server maintenance period:

Client Request:

DELETE /documents/456 HTTP/1.1
Host: docs.example.org
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

Server Response:

HTTP/1.1 307 Temporary Redirect
Location: https://backup-docs.example.org/documents/456
Content-Type: text/html
Content-Length: 189

<!DOCTYPE html>
<html>
<head>
  <title>Temporary Redirect</title>
</head>
<body>
  <p>This server is undergoing maintenance. Your request will be redirected to our backup server.</p>
</body>
</html>

Summary

The 307 Temporary Redirect status code is a valuable tool for preserving HTTP method semantics during temporary redirects. It ensures that clients maintain the original request method when following the redirect, which is crucial for proper functioning of web applications and APIs that rely on specific HTTP methods. This status code is particularly useful during server maintenance, load balancing, or failover scenarios where requests need to be temporarily redirected while maintaining their original intent and payload.