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.
Location header pointing to the new resourcePOST requestsWhen a server creates a new resource, it responds with status code 201. The response body may include details of the created resource.
// 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
});
});
The server responds with status code 201 Created and returns the newly created user object in JSON format.
Below is a simulated API environment. Enter a name and click "Create User" to simulate a POST request and see the 201 Created response.
// 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);
};
Location header if possible