HTTP Methods, Request, Response

লেসন ৩৪ / ৩৯

HTTP Request কাঠামো

POST /api/users HTTP/1.1           ← Request Line
Host: api.example.com               ← Headers
Content-Type: application/json
Authorization: Bearer TOKEN
                                    ← (খালি লাইন)
{                                    ← Body (payload)
    "name": "করিম",
    "email": "k@mail.com"
}

HTTP Response

HTTP/1.1 201 Created                ← Status
Content-Type: application/json
Content-Length: 75

{
    "success": true,
    "data": { "id": 42, "name": "করিম" }
}

HTTP Methods

MethodকাজBodyIdempotent
GETপড়া❌✅
POSTতৈরি✅❌
PUTসম্পূর্ণ update✅✅
PATCHআংশিক update✅❌
DELETEমুছেসাধারণত না✅
OPTIONSCORS preflight❌✅

RESTful URL

GET    /api/users              → সব
GET    /api/users/42            → একজন
POST   /api/users               → নতুন
PUT    /api/users/42            → update
DELETE /api/users/42            → মুছে
GET    /api/users/42/orders     → nested

❌ /api/getProducts, /api/product/create
✅ /api/products

Status Codes

2xx Success

  • 200 OK
  • 201 Created
  • 204 No Content

4xx Client Error

  • 400 Bad Request
  • 401 Unauthorized (login দরকার)
  • 403 Forbidden (অনুমতি নেই)
  • 404 Not Found
  • 409 Conflict (duplicate)
  • 422 Unprocessable (validation)
  • 429 Too Many Requests

5xx Server Error

  • 500 Internal Server Error
  • 502 Bad Gateway
  • 503 Service Unavailable

Payload (Body) Format

// JSON (সবচেয়ে জনপ্রিয়) ⭐
Content-Type: application/json
{
    "name": "করিম",
    "age": 25
}

// Form URL-encoded (HTML form)
Content-Type: application/x-www-form-urlencoded
name=করিম&age=25

// Multipart (file upload)
Content-Type: multipart/form-data

Standard Response Format

// ✅ Success
{
    "success": true,
    "data": { "id": 42, "name": "করিম" },
    "message": "User created"
}

// ✅ List with pagination
{
    "success": true,
    "data": [...],
    "meta": {
        "total": 150, "page": 1,
        "per_page": 10, "total_pages": 15
    }
}

// ❌ Error
{
    "success": false,
    "error": {
        "code": "VALIDATION_FAILED",
        "message": "ইমেইল সঠিক নয়",
        "fields": { "email": "Invalid" }
    }
}

Important Headers

// Request
Content-Type: application/json
Accept: application/json
Authorization: Bearer TOKEN
User-Agent: MyApp/1.0

// Response
Content-Type: application/json; charset=utf-8
Cache-Control: max-age=3600
Access-Control-Allow-Origin: *    // CORS