REST Lens
← Back to All Rules
Rule 34

Code 201 Create

Category: Status Codes

Description

Resource creation operations (POST requests) should return a 201 Created status code instead of other 2XX codes when successfully creating a new resource.

What It Checks

This rule validates that POST endpoints that create new resources return the appropriate 201 status code. It checks:

  • All POST operations in the API specification
  • Uses LLM analysis to determine if the operation is actually creating a resource (based on path, method, summary, and description)
  • Verifies the presence of a 201 response definition
  • Flags operations that return other specific 2XX codes (200, 202, 204, etc.) instead of 201

The rule specifically looks for POST operations that:

  1. Are identified as creation operations through semantic analysis
  2. Define specific 2XX status codes (other than the generic "2XX" placeholder)
  3. Do NOT include a 201 response

Why It Matters

Using 201 Created for resource creation provides clear, standardized communication:

  • Semantic clarity: 201 explicitly signals that a new resource was created, unlike 200 which is generic success
  • REST compliance: Following HTTP specification standards (RFC 7231) ensures interoperability
  • Client expectations: API consumers expect 201 for creation and may parse responses differently based on status codes
  • Location header: 201 responses conventionally include a Location header pointing to the new resource, which 200 responses typically don't

Examples

Good ✅

paths:
  /users:
    post:
      summary: Create a new user
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                email:
                  type: string
      responses:
        '201':
          description: User created successfully
          headers:
            Location:
              schema:
                type: string
              description: URI of the created user
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'

  /orders:
    post:
      summary: Create a new order
      responses:
        '201':
          description: Order created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Order'

  /projects/{projectId}/tasks:
    post:
      summary: Add a new task to the project
      responses:
        '201':
          description: Task created successfully

Bad ❌

paths:
  /users:
    post:
      summary: Create a new user
      responses:
        '200':  # Should be 201
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'

  /orders:
    post:
      summary: Create an order
      responses:
        '200':  # Should be 201
          description: Order created
        '400':
          description: Invalid input

  /projects/{projectId}/tasks:
    post:
      summary: Create task
      responses:
        '204':  # Should be 201 (204 means no content)
          description: Task created

Configuration

This rule has no configuration options. It is either enabled or disabled.

Exceptions

The rule allows non-201 responses in these cases:

  1. Generic 2XX placeholder: If only "2XX" is defined (not specific codes like 200, 202, etc.), no violation is raised
  2. Non-creation POST operations: POST endpoints that don't create resources (e.g., /search, /validate, /execute) won't trigger violations
  3. POST with 201 defined: If 201 is present, even alongside other 2XX codes, no violation occurs

How to Fix

  1. Change the success status code to 201 for creation endpoints:

    # Before
    responses:
      '200':
        description: User created
    
    # After
    responses:
      '201':
        description: User created successfully
    
  2. Add Location header to point to the created resource:

    responses:
      '201':
        description: User created successfully
        headers:
          Location:
            schema:
              type: string
            description: URI of the newly created user (/users/{userId})
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/User'
    
  3. Return the created resource in the response body:

    responses:
      '201':
        description: Resource created
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Resource'
    
  4. Ensure consistency across all creation endpoints in your API

Related Rules

  • Rule 35: Code 200 OK - Proper use of 200 for successful operations
  • Rule 36: Code 204 No Content - When to use 204 for successful operations with no response body
  • Rule 42: POST Method Usage - Proper usage of POST for creation and other operations