Product knowledge · Beginner · Request anatomy

Một URL không phải một route pattern.

Tách từng thành phần của request để biết Core parse và giữ wire facts ở đâu, còn application decode, validate và gắn business meaning ở đâu.

01 · One concrete request

Bóc request thành các phần độc lập

GET http://127.0.0.1:8080/orders/A%2FB?tag=one&flag&tag=&tag=two#receipt
    └── scheme + authority ──┘ └ path ┘ └──────── query ────────┘ └ browser fragment

route declaration: GET /orders/{id}
wire request target: /orders/A%2FB?tag=one&flag&tag=&tag=two
captured id: A%2FB

Client URL

Scheme, authority, path và query dùng ở browser/curl/client. Fragment chỉ ở browser, không gửi tới server.

Listener

Bind address + port là startup configuration, không nằm trong route declaration.

Method

Route và request đều có method; comparison case-sensitive theo contract.

Request target

Core parse path/query, áp dụng bounds và chiếu facts qua connector.

02 · Route declaration

Route chọn handler bằng method + path pattern

PatternNghĩa
/Exact root.
/healthExact static path.
/orders/{id}Một nonempty whole-segment capture.
/orders/{orderId}/items/{itemId}Hai capture theo path order.
/orders/{id}/Trailing slash là một phần của pattern.

Không khai báo full URL, query template, Express :id, regex, optional segment hay catch-all *. Static segment thắng capture ở vị trí khác nhau đầu tiên; ambiguous same-structure patterns bị reject, không giải bằng registration order.

03 · Path and captures

Encoded không có nghĩa là decoded text

/items/a%2Fb
Capture là a%2Fb; encoded slash vẫn ở trong một segment vì boundary được xác định trước decode.
/users vs /users/
Hai path khác nhau; không giả định redirect của framework.
Application step
Percent-decode, kiểm tra UTF-8, parse business type và validate policy một cách tường minh.
Không làm
Không decode capture rồi feed ngược vào routing; không dùng string conversion thay percent decoding.

04 · Ordered query entries

Presence và thứ tự là dữ liệu

Wire inputMeaning
không có flagKey absent.
?flagKey present, value absent.
?flag=Key present, empty value.
?tag=one&tag=twoHai ordered entries; không overwrite thành một map value.
?q=a+bEncoded value giữ +; không tự form-decode thành space.
?q=a%26bMột value chứa encoded &.

Query không chọn route khác. Handler sở hữu business behavior; khi dựng query, dùng URL encoder của client thay vì nối untrusted text.

05 · Headers

Lookup case-insensitive; values không nhất thiết là một map

Name

Content-Type và content-type lookup cùng tên theo HTTP rule.

Duplicates

Dùng ordered/all-values view khi duplicates có nghĩa; đừng collapse nếu connector giữ chúng.

Bounds

Core kiểm tra count/bytes trước application use. Limit failure có thể xảy ra trước handler.

Safety

Không reflect arbitrary inbound headers, cookies, authorization hoặc credential vào response/log.

06 · Body and trailers

Payload bytes không tự trở thành JSON

Buffered body
Complete bounded bytes đã được connector chiếu; application chọn media type, decoder, schema và business validation.
Streaming body
Chunks, backpressure, cancellation và borrowed lifetime riêng; thuộc Advanced.
Trailers
Fields đến sau body data/EOF; không đọc như initial headers hoặc trước khi stream kết thúc.
Response
Application chọn status/headers/body; Core sở hữu framing. Không hand-write Content-Length/Transfer-Encoding để giành framing.

07 · Building outbound intent

Route pattern không phải outbound path

logical target: billing.api
connection endpoint: 10.0.2.15:8443
HTTP authority / TLS identity: billing.internal
method: POST
origin-form target: /charges/customer-7?notify=true
deadline: finite

wrong: /charges/{customerId}
wrong: https://billing.internal/charges/... in a path/query-only field

Connect destination, HTTP authority và TLS peer identity là ba setting riêng. Successful submit chỉ nghĩa admitted; đọc matching terminal để phân biệt HTTP 4xx/5xx response với cancellation, deadline hoặc transport failure. Retry/idempotency vẫn thuộc application.

08 · Request checklist

Trước khi chuyển sang API của ngôn ngữ