httpx

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpx 提供管理 API 共用的 JSON 读写与错误响应。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request) string

ClientIP 返回请求的对端 IP(不信任转发头,管理端口直接面向用户)。

func Error

func Error(w http.ResponseWriter, status int, format string, args ...any)

Error 输出错误 JSON。

func OK

func OK(w http.ResponseWriter, v any)

OK 输出 200 JSON。

func ReadJSON

func ReadJSON(r *http.Request, v any) error

ReadJSON 解析请求体到 v,拒绝未知字段以尽早暴露前后端不一致。 要求 Content-Type 为 application/json:浏览器跨源发送该类型会触发预检,可阻止表单类 CSRF。

func SameOrigin

func SameOrigin(next http.Handler) http.Handler

SameOrigin 拒绝来自其他源的状态变更请求(同站不同端口、兄弟子域同样被拒绝)。 浏览器会为跨源请求带上 Origin 或 Sec-Fetch-Site;命令行工具通常两者都不带,予以放行。

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

WriteJSON 以指定状态码输出 JSON。

Types

type ErrorBody

type ErrorBody struct {
	Error string `json:"error"`
}

ErrorBody 是统一的错误响应结构。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL