Documentation
¶
Overview ¶
Package httpx 提供管理 API 共用的 JSON 读写与错误响应。
Index ¶
- func ClientIP(r *http.Request) string
- func Error(w http.ResponseWriter, status int, format string, args ...any)
- func OK(w http.ResponseWriter, v any)
- func ReadJSON(r *http.Request, v any) error
- func SameOrigin(next http.Handler) http.Handler
- func WriteJSON(w http.ResponseWriter, status int, v any)
- type ErrorBody
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
func Error(w http.ResponseWriter, status int, format string, args ...any)
Error 输出错误 JSON。
func ReadJSON ¶
ReadJSON 解析请求体到 v,拒绝未知字段以尽早暴露前后端不一致。 要求 Content-Type 为 application/json:浏览器跨源发送该类型会触发预检,可阻止表单类 CSRF。
func SameOrigin ¶
SameOrigin 拒绝来自其他源的状态变更请求(同站不同端口、兄弟子域同样被拒绝)。 浏览器会为跨源请求带上 Origin 或 Sec-Fetch-Site;命令行工具通常两者都不带,予以放行。