info http

HTTP 201 Created

Understanding HTTP 201 Created status code - indicates a new resource has been successfully created as a result of the request.

What It Means

HTTP 201 Created indicates that the request has been fulfilled and has resulted in one or more new resources being created. The primary resource created is identified by either a Location header field in the response or by the request URI.

This status code is typically returned in response to a POST request, or sometimes a PUT request when a resource is created at the specified URI.

Common Causes

  • A successful POST request that creates a new resource (user, record, file)
  • A PUT request that creates a new resource at a specific URI
  • Successful file upload creating a new resource on the server
  • Successful registration or sign-up action

How to Fix

HTTP 201 is a success status code. Here are best practices for returning it:

// Express.js - Correct usage of 201
app.post('/api/users', async (req, res) => {
  const user = await User.create(req.body);

  res.status(201)
    .location(`/api/users/${user.id}`)
    .json({
      id: user.id,
      name: user.name,
      email: user.email,
      createdAt: user.createdAt
    });
});
# Django REST Framework
class UserViewSet(viewsets.ModelViewSet):
    def create(self, request):
        serializer = UserSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(
            serializer.data,
            status=status.HTTP_201_CREATED,
            headers={'Location': f'/api/users/{serializer.data["id"]}'}
        )
// Go net/http
func createUserHandler(w http.ResponseWriter, r *http.Request) {
    user, err := createUser(r.Body)
    if err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    w.Header().Set("Location", fmt.Sprintf("/api/users/%d", user.ID))
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(http.StatusCreated)
    json.NewEncoder(w).Encode(user)
}

Best practices:

  • Always include a Location header pointing to the newly created resource
  • Return the created resource in the response body
  • Use 201 only for POST (or PUT) requests that create new resources — not for updates
  • HTTP 200 - OK: Use for general successful requests that don’t create new resources.
  • HTTP 204 - No Content: Use when the action succeeds but there is no response body.
  • HTTP 409 - Conflict: Returned when creation fails due to a conflict with existing resources.