HTTP 201 Created indicates that the request has been fulfilled and a new resource has been created. The server SHOULD return a Location header with the URI of the newly created resource and include a representation of the resource (or a reference to it) in the response body. This status code is fundamental to RESTful API design — it distinguishes successful creation from other success operations, enabling clients to immediately navigate to or reference the new resource.
Response includes the status code, standard headers (including Content-Type), and a small diagnostic JSON body describing the request and returned status.
Simulator URL (copy in the app after load — not a normal link):
https://httpstatus.com/api/status/201
Example request:
curl -i "https://httpstatus.com/api/status/201"The request has been fulfilled and resulted in a new resource being created.
On this code, Inspector focuses on semantics, headers, and correctness warnings that commonly affect clients and caches.
201 Created (RFC 7231 Section 6.3.2) is the correct response for POST requests that create a new resource, and for PUT requests that create a resource at the specified URI when it didn't previously exist. The response SHOULD include a Location header pointing to the new resource's canonical URI and an ETag for the new representation. Key detail: 201 responses are NOT cacheable by default (unlike 200). The response body typically contains the created resource with server-generated fields (id, timestamps, computed defaults) so the client doesn't need an additional GET. For idempotent creation patterns, combine 201 with an Idempotency-Key header to prevent duplicate resource creation on retries.
app.post('/api/users', async (req, res) => {
try {
const user = await db.createUser(req.body);
res.status(201)
.location(`/api/users/${user.id}`)
.json({
id: user.id,
name: user.name,
email: user.email,
createdAt: user.createdAt
});
} catch (err) {
if (err.code === 'DUPLICATE_EMAIL') {
return res.status(409).json({ error: 'Email already exists' });
}
res.status(500).json({ error: 'Internal server error' });
}
});from fastapi import FastAPI
from fastapi.responses import JSONResponse
@app.post('/api/users', status_code=201)
async def create_user(user: UserCreate):
db_user = await db.create_user(user)
return JSONResponse(
status_code=201,
content=db_user.dict(),
headers={'Location': f'/api/users/{db_user.id}'}
)@PostMapping("/api/users")
public ResponseEntity<User> createUser(
@Valid @RequestBody CreateUserRequest req) {
User user = userService.create(req);
URI location = URI.create("/api/users/" + user.getId());
return ResponseEntity.created(location).body(user);
}func createUserHandler(w http.ResponseWriter, r *http.Request) {
var req CreateUserRequest
json.NewDecoder(r.Body).Decode(&req)
user, err := db.CreateUser(req)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Location", "/api/users/"+user.ID)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated) // 201
json.NewEncoder(w).Encode(user)
}