Creating Resources with POST
A typical creation endpoint is POST /users. The client submits a representation without choosing the server-generated identifier.
A successful creation commonly returns 201 Created and a Location header identifying the new resource.
POST /users
Content-Type: application/json
{"name":"Suriya","email":"suriya@example.com"}
Response:
HTTP/1.1 201 Created
Location: /users/42
Content-Type: application/json
{"id":42,"name":"Suriya","email":"suriya@example.com"}
The server should validate the request, enforce business constraints, persist the resource, and return a representation appropriate for the API contract.
Express Resource Creation
app.post('/users', async (req, res) => {
const user = await userService.create(req.body);
res.status(201).location(`/users/${user.id}`).json(user);
});💡 The endpoint accepts a representation, creates the resource, returns 201, and communicates its URI through Location.