← Back to Chapters

HTTP 201 Created

? HTTP 201 Created

? Quick Overview

The 201 Created status code indicates that a request has been successfully processed and resulted in the creation of a new resource on the server. It is commonly returned after successful POST requests in RESTful APIs.

? Key Concepts

  • Belongs to the 2xx Success HTTP status code family
  • Confirms that a new resource was created
  • Usually includes a Location header pointing to the new resource
  • Most commonly used with POST requests

? Syntax / Theory

When a server creates a new resource, it responds with status code 201. The response body may include details of the created resource.

? Code Example(s)

? View Code Example
// Express.js route that returns HTTP 201 when a resource is created
app.post("/users", (req, res) => {
  const user = req.body;
  res.status(201).json({
    message: "User created successfully",
    data: user
  });
});

? Live Output / Explanation

Server Response

The server responds with status code 201 Created and returns the newly created user object in JSON format.

? Interactive Example

Below is a simulated API environment. Enter a name and click "Create User" to simulate a POST request and see the 201 Created response.

? View Source Code
// Simulated Client Request
const createUser = async (name) => {
  console.log("POST /users", { name });
  
  // Simulate Server Delay
  setTimeout(() => {
    return {
      status: 201,
      statusText: "Created",
      body: { id: 123, name: name, createdAt: new Date() }
    };
  }, 1000);
};

?️ API Simulator

 
 

? Use Cases

  • User registration systems
  • Creating orders in e-commerce applications
  • Uploading files or media records
  • Adding new database records via APIs

✅ Tips & Best Practices

  • Always return 201 only when a new resource is actually created
  • Include a Location header if possible
  • Return meaningful response data for better client usability

? Try It Yourself

  1. Create a simple API endpoint that accepts a POST request
  2. Return status code 201 after saving data
  3. Test the response using Postman or browser DevTools