HTTP 303 See Other

Overview

The HTTP 303 See Other status code is used to redirect clients to a different URI, typically after a PUT or POST operation, suggesting that the client should perform a GET request to the provided URI.

Purpose

The HTTP 303 response is primarily used to prevent clients from re-submitting a form when refreshing a web page. It indicates that the response to the request can be found at a different URI and should be retrieved using a GET method.

Usage

Client Behavior:

  1. Send Request: The client sends a PUT or POST request to a resource.
  2. Receive Response: The client receives the HTTP 303 status code, directing it to a different URI where the response can be found.

Server Behavior:

  1. Process Request: The server processes the PUT or POST request.
  2. Send Response: The server sends a 303 See Other response, providing the URI where the client should now perform a GET request.

Scenarios

  • Form Submissions: Commonly used after a form submission to redirect clients, preventing form re-submission on refresh.
  • Resource Creation: After creating a resource with POST, redirecting to a page showing the creation result.

Sequence Diagram

Illustrating the process for an HTTP 303 response:

sequenceDiagram
    participant Client
    participant Server as Web Server

    Note over Client: Client submits data via POST
    Client->>Server: POST /submit-data HTTP/1.1
    Note over Server: Server processes data
    Server->>Client: HTTP/1.1 303 See Other
    Server->>Client: Location: /confirmation-page

Curl Request and Response Example

Sending a POST request using Curl and being redirected with a 303 response:

curl -i -X POST -d "data=example" http://example.com/submit-data
# Expected response: HTTP/1.1 303 See Other
# Location: http://example.com/confirmation-page

PHP cURL Request and Response Example

PHP script using cURL to handle a POST request followed by a 303 redirection:

<?php
$ch = curl_init('http://example.com/submit-data');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => 'example'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 303) {
    $newUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
    echo "See other page at: " . $newUrl;
}
curl_close($ch);
?>

Python Request and Response Example

Python script to send a POST request and handle a 303 See Other response:

import requests
response = requests.post('http://example.com/submit-data', data={'data': 'example'})
if response.status_code == 303:
    print("See other page at:", response.headers['Location'])

Apache Configuration for HTTP 303 See Other

Configuring Apache to handle a 303 redirection after a POST operation:

<VirtualHost *:80>
    ServerName example.com
    # Redirect after a POST request
    <Location "/submit-data">
        Redirect 303 /confirmation-page
    </Location>
</VirtualHost>

NGINX Configuration for HTTP 303 See Other

Setting up NGINX to handle a 303 redirection following a POST request:

server {
    listen 80;
    server_name example.com;
    location /submit-data {
        return 303 http://example.com/confirmation-page;
    }
}

HTTP 302 Found HTTP 304 Not Modified


 

Free Weekly

Newsletter

Join my weekly newsletter for the latest in tech! You'll get neat coding tricks, trend updates, career advice, SaaS reviews, crypto, bitcoin, and financial tips. All straight to your inbox, designed to keep you ahead.