What is the difference between POST and PUT in HTTP?

Learn what is the difference between post and put in http? with practical examples, diagrams, and best practices. Covers http, rest, post development techniques with visual explanations.

POST vs. PUT: Understanding HTTP Method Differences

Hero image for What is the difference between POST and PUT in HTTP?

Explore the fundamental distinctions between the HTTP POST and PUT methods, their idempotence, and when to use each for effective RESTful API design.

In the world of HTTP and RESTful APIs, POST and PUT are two of the most commonly used methods for sending data to a server. While both are used to submit data, they serve distinct purposes and have different implications for how resources are handled on the server-side. Understanding these differences is crucial for designing robust, predictable, and maintainable APIs.

The POST Method: Creating New Resources

The POST method is primarily used to create new resources on the server. When you send a POST request, you are typically sending data that the server will use to generate a new entry or record. The server is responsible for assigning a unique identifier (URI) to this newly created resource. Each POST request, even with identical data, is generally expected to create a new resource, making it a non-idempotent operation.

POST /users HTTP/1.1
Host: api.example.com
Content-Type: application/json

{
    "name": "John Doe",
    "email": "john.doe@example.com"
}

Example of a POST request to create a new user.

The PUT Method: Updating or Replacing Resources

The PUT method is used to update an existing resource or create a resource if it does not already exist at a specified URI. Unlike POST, with PUT, the client typically provides the complete URI for the resource being modified or created. A key characteristic of PUT is its idempotence. This means that making the same PUT request multiple times will have the same effect as making it once – it will result in the same resource state on the server. If the resource exists, it's updated; if not, it's created at the specified URI.

PUT /users/123 HTTP/1.1
Host: api.example.com
Content-Type: application/json

{
    "id": "123",
    "name": "Jane Doe",
    "email": "jane.doe@example.com"
}

Example of a PUT request to update an existing user with ID 123.

Idempotence Explained

Idempotence is a crucial concept when discussing POST and PUT. An operation is idempotent if executing it multiple times produces the same result as executing it once.

  • PUT is idempotent: If you send the same PUT request multiple times to /users/123 with the same data, the user with ID 123 will remain in the same state after each request. It will either be created once or updated once, and subsequent identical requests will not change its state further.
  • POST is not idempotent: If you send the same POST request multiple times to /users, it will typically create multiple new user resources, each with a unique ID, even if the submitted data is identical. This is because POST is designed for creating new entities, and each submission is treated as a request for a new creation.
flowchart TD
    A[Client Request]
    subgraph POST Flow
        B(POST /resources)
        B --> C{Server Creates New Resource}
        C --> D[Assigns New URI]
        D --> E[Returns 201 Created with Location Header]
    end
    subgraph PUT Flow
        F(PUT /resources/ID)
        F --> G{Resource ID Exists?}
        G -->|Yes| H[Server Updates Resource at ID]
        G -->|No| I[Server Creates Resource at ID]
        H --> J[Returns 200 OK or 204 No Content]
        I --> J
    end
    A --> B
    A --> F

Flowchart illustrating the difference in resource handling between POST and PUT.

When to Use Which Method

Choosing between POST and PUT depends on the desired outcome and the nature of the resource interaction:

  • Use POST when:

    • You are creating a new resource, and the server is responsible for assigning its URI (e.g., adding a new item to a collection).
    • You are submitting data to be processed, such as sending an order, uploading a file, or performing a transaction.
    • The operation is not idempotent (repeated requests should result in multiple new resources or actions).
  • Use PUT when:

    • You are updating an existing resource at a known URI.
    • You are creating a resource at a client-specified URI (e.g., uploading a file to a specific path).
    • The operation needs to be idempotent (repeated requests should have the same effect as a single request).