Structuring Request Payloads

1. Structuring Request Headers

HeaderPurpose
Content-TypeFormat of request body
AcceptDesired response format
AuthorizationCredentials
Content-LengthBody size in bytes
User-AgentClient identifier
Accept-EncodingSupported compression (gzip, br)
Accept-LanguagePreferred locale
If-Match / If-None-MatchConditional requests
Idempotency-KeySafe retry key for POST

2. Structuring Request Body

MethodBody Convention
POST (create)Resource representation without server-generated fields (id, createdAt)
PUT (replace)Complete resource representation
PATCH (partial)Only fields to change (Merge Patch) or operations array (JSON Patch)
DELETEUsually empty

Example: Well-Structured POST Body

{
  "name": "Alice Smith",
  "email": "alice@example.com",
  "preferences": {
    "newsletter": true,
    "locale": "en-US"
  },
  "tags": ["beta-tester", "premium"]
}

3. Using Query Parameters

PatternExample
Single value?status=active
Multi-value (repeat)?tag=red&tag=blue
Multi-value (CSV)?tags=red,blue,green
Range?createdAt[gte]=2026-01-01
Sort?sort=-createdAt,name

4. Using Path Parameters

Use ForExample
Identifying resource/users/{userId}
Hierarchy traversal/users/{userId}/orders/{orderId}
Required fields onlyOptional → use query string

5. Using Request Body for Complex Data

ScenarioWhy Body
Nested objects, arraysQuery strings flatten poorly
Large payloads (> 2KB)URL length limits (~2048 chars)
Sensitive dataURLs logged; bodies usually not
Binary dataCannot be represented in URL
Note: Avoid GET with body — many proxies strip it. For complex queries, use POST /searches with query body.

6. Handling Form Data

Content-TypeFormatUse
application/x-www-form-urlencodedname=Alice&email=a%40x.comSimple HTML forms
multipart/form-dataBoundary-separated partsFile uploads, mixed fields

7. Handling Multipart Requests

Example: Multipart Upload

POST /api/avatars HTTP/1.1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary

------WebKitFormBoundary
Content-Disposition: form-data; name="userId"

42
------WebKitFormBoundary
Content-Disposition: form-data; name="avatar"; filename="me.png"
Content-Type: image/png

<binary data>
------WebKitFormBoundary--
Part HeaderPurpose
Content-DispositionField name + optional filename
Content-TypePer-part MIME type

8. Validating Request Data

LayerValidation
SchemaRequired fields, types, formats (JSON Schema, OpenAPI)
BusinessCross-field rules, uniqueness, state transitions
SecuritySanitization (XSS), injection prevention
AuthorizationField-level permission checks

9. Handling Request Size Limits

Limit TypeTypical Value
URL length2048 chars (varies by browser/server)
JSON body1-10 MB (express default 100KB)
Multipart upload10-100 MB (configurable)
Streaming uploadUnlimited (chunked)
Warning: Set explicit limits to prevent DoS via huge payloads. Return 413 Payload Too Large.

10. Using Custom Headers

HeaderUse Case
X-Request-IDTrace requests across services
X-Tenant-IDMulti-tenant routing
Idempotency-KeySafe POST retries
X-Client-VersionClient app version for analytics