KAHIBARO
Discord Login Register

POST Requests

Understanding POST Requests

POST requests are used to send data to the server to create or submit something new. In REST APIs, POST is one of the most common methods you will use.

This chapter focuses only on what is specific to POST in REST APIs. Concepts like general HTTP methods, status codes, and validation are covered in their own chapters, so we will only touch them when needed for context.


What POST Is Used For

In REST-style APIs, the usual meaning of a POST request is:

Typical examples:

Rule: In a REST API, POST is usually used on a collection URL (like /users) to create a new resource inside that collection.


Structure of a POST Request

A POST request has:

Example of a raw HTTP POST request:

http
POST /users HTTP/1.1
Host: api.example.com
Content-Type: application/json
Authorization: Bearer <token>
{
  "email": "alice@example.com",
  "password": "Secret123!",
  "name": "Alice"
}

Key parts here:


PartExampleMeaning
MethodPOSTWe are sending data to the server
URL path/usersEndpoint that receives new user data
HeaderContent-Type: application/jsonBody format is JSON
Header (optional)Authorization: Bearer <token>Who is making the request
BodyJSON object with user informationData to create a new user

Common Uses of POST in REST APIs

1. Creating Resources

Most commonly, POST is used to create a new item in a collection.

Example: Creating a blog post

http
POST /posts HTTP/1.1
Host: api.example.com
Content-Type: application/json
{
  "title": "My first post",
  "content": "Hello backend world!",
  "tags": ["intro", "backend"]
}
http
HTTP/1.1 201 Created
Location: /posts/123
Content-Type: application/json
{
  "id": 123,
  "title": "My first post",
  "content": "Hello backend world!",
  "tags": ["intro", "backend"],
  "created_at": "2026-08-26T12:34:56Z"
}

Points to notice:

Rule: When a POST successfully creates a resource, prefer status code 201 Created and include the new resource or at least its URI.

2. Submitting Forms

Some actions only submit data for processing and do not create a resource that you later fetch.

Example: Contact form

http
POST /contact HTTP/1.1
Content-Type: application/json
{
  "name": "Alice",
  "email": "alice@example.com",
  "message": "Please contact me back."
}
http
HTTP/1.1 200 OK
Content-Type: application/json
{
  "status": "received",
  "ticket_id": "T-9982"
}

Although this could be considered "creating a ticket", sometimes the client never needs a separate /tickets resource. The key idea is that POST is used to submit data, and the response can be anything meaningful.

3. Login and Other Actions

Example: User login

http
POST /login HTTP/1.1
Content-Type: application/json
{
  "email": "alice@example.com",
  "password": "Secret123!"
}
http
HTTP/1.1 200 OK
Content-Type: application/json
{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "token_type": "bearer"
}

This POST is not creating a user. It is creating a token or a session, or simply performing an authentication action.


POST vs GET, PUT, and PATCH

You will see POST mentioned a lot together with other HTTP methods. Here we only compare how POST is different, not fully define everything.

POST vs GET

AspectGETPOST
PurposeRetrieve dataSend data, usually to create or submit
Has a body?Usually no bodyUsually has a body
Side effectsShould not change stateOften changes state (creates or triggers)
CachingOften cachedUsually not cached

Rule: Do not use GET to change data. Use POST (or PUT/PATCH/DELETE) for actions that modify server state.

POST vs PUT

A very simple way to remember:

For example:

POST vs PATCH

For example:

Idempotency and POST

An operation is idempotent if running it many times with the same input has the same effect as running it once.

Example of a non idempotent POST:

http
POST /orders
{
  "product_id": 42,
  "quantity": 1
}

If the client accidentally sends this same request 3 times, you might create 3 identical orders.

Sometimes you want idempotent behavior with POST, for example when creating payments. One common solution is to use an idempotency key:

http
POST /payments
Idempotency-Key: 43e55b3f-9e7d-4bc8-9c48-7ac4c4a436ca
Content-Type: application/json
{
  "amount": 1000,
  "currency": "USD",
  "customer_id": 7
}

If the server sees the same Idempotency-Key again, it returns the previous result instead of creating a new payment. Idempotency keys are covered more in the advanced idempotency chapter, but you should know that they are often used with POST.


Request Bodies in POST

The most important part of a POST is usually the request body.

Common Body Types

Content-TypeDescriptionExample usage
application/jsonJSON object or arrayMost modern APIs
application/x-www-form-urlencodedKey value fields, URL encodedHTML forms
multipart/form-dataMixed data, for example text and filesFile uploads
text/plainPlain textVery simple webhooks

A POST that creates a resource in a JSON API usually uses application/json.

Example: Create a comment

http
POST /posts/123/comments HTTP/1.1
Content-Type: application/json
{
  "author": "Bob",
  "text": "Nice article!"
}

The server will parse that JSON body, validate it, and store it.


Validation and Error Responses with POST

When you accept data from clients, validation is critical. The details are in the validation and error response chapters, but we will look at how this typically appears with POST.

Example: Validation Failure

Request:

http
POST /users HTTP/1.1
Content-Type: application/json
{
  "email": "not-an-email",
  "password": "123",
  "name": ""
}

The server checks:

The response could look like this:

http
HTTP/1.1 400 Bad Request
Content-Type: application/json
{
  "error": "Validation failed",
  "details": {
    "email": ["Invalid email address"],
    "password": ["Password must be at least 8 characters"],
    "name": ["Name is required"]
  }
}

Common status codes for POST errors:


Status codeMeaning
400Invalid data or malformed request
401Not authenticated
403Authenticated but not allowed
404Parent resource not found (for example /posts/999/comments)
409Conflict, for example duplicate email
422Unprocessable Entity, often used for validation
500Internal server error

Typical Responses for POST

Different success scenarios usually use different status codes.

201 Created

Most common when a resource is created.

http
HTTP/1.1 201 Created
Location: /users/42
Content-Type: application/json
{
  "id": 42,
  "email": "alice@example.com",
  "name": "Alice"
}

200 OK

Used when the server returns some useful information, but not necessarily a new resource that fits a collection.

Examples:

http
HTTP/1.1 200 OK
Content-Type: application/json
{
  "status": "success",
  "message": "Your message was sent"
}

204 No Content

Used when the server successfully processed the request but does not need to return a body.

Example: A POST that triggers an asynchronous job

http
POST /jobs/rebuild-search-index HTTP/1.1

Response:

http
HTTP/1.1 204 No Content

Examples of POST Endpoints in a Simple API

Imagine a simple task management REST API.

1. Create a Task

Request

http
POST /tasks HTTP/1.1
Content-Type: application/json
{
  "title": "Write chapter about POST",
  "description": "Explain POST requests for beginners",
  "due_date": "2026-08-30"
}

Successful response

http
HTTP/1.1 201 Created
Location: /tasks/101
Content-Type: application/json
{
  "id": 101,
  "title": "Write chapter about POST",
  "description": "Explain POST requests for beginners",
  "due_date": "2026-08-30",
  "completed": false,
  "created_at": "2026-08-27T09:00:00Z"
}

2. Mark a Task as Completed with a POST Action

Sometimes there is debate about whether to use POST or PATCH for actions like "complete task". One common pattern is to use a subresource or action URL with POST.

Request

http
POST /tasks/101/complete HTTP/1.1

Response

http
HTTP/1.1 200 OK
Content-Type: application/json
{
  "id": 101,
  "title": "Write chapter about POST",
  "completed": true
}

Here POST is used to trigger an action: "complete this task".

3. Bulk Create with POST

You can also accept arrays in POST bodies.

Request

http
POST /tasks/bulk HTTP/1.1
Content-Type: application/json
[
  { "title": "Read REST chapter" },
  { "title": "Practice POST requests" },
  { "title": "Implement /tasks endpoint" }
]

Response

http
HTTP/1.1 201 Created
Content-Type: application/json
[
  {
    "id": 201,
    "title": "Read REST chapter",
    "completed": false
  },
  {
    "id": 202,
    "title": "Practice POST requests",
    "completed": false
  },
  {
    "id": 203,
    "title": "Implement /tasks endpoint",
    "completed": false
  }
]

Security Considerations for POST

Even in a basic introduction, you should be aware of some security aspects:

  1. Never trust client input
    • Always validate and sanitize POST bodies
    • Check for required fields and correct formats
  2. Use HTTPS
    • POST bodies often contain sensitive data like passwords or personal info
    • Without HTTPS, this can be intercepted
  3. Limit request size
    • Attackers can send huge request bodies to exhaust memory
    • Most frameworks and servers let you set a maximum body size
  4. Authentication and authorization
    • Some POST endpoints must be accessible only by logged in users
    • For example, POST /admin/users should be protected

You will see these topics in more detail in the security and authentication chapters, but they are heavily involved with POST requests since POST is used to change things.


Putting It All Together: A Full Example

Imagine we create a small REST API for notes.

Create a Note

Request

http
POST /notes HTTP/1.1
Content-Type: application/json
Authorization: Bearer <token>
{
  "title": "Shopping list",
  "content": "Milk, bread, eggs"
}

Response success

http
HTTP/1.1 201 Created
Location: /notes/10
Content-Type: application/json
{
  "id": 10,
  "title": "Shopping list",
  "content": "Milk, bread, eggs",
  "owner_id": 3,
  "created_at": "2026-08-27T10:00:00Z"
}

Response validation error

http
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/json
{
  "error": "Validation failed",
  "details": {
    "title": ["Title is required"]
  }
}

Response unauthorized

http
HTTP/1.1 401 Unauthorized
Content-Type: application/json
{
  "error": "Not authenticated"
}

From this one endpoint you can see:

Summary

You will use POST requests constantly when building real REST APIs, especially for creation and submission endpoints.

Views: 8

Comments

Please login to add a comment.

Don't have an account? Register now!