body

package
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// Text 纯文本类型
	Text = "text/plain"
	// JavaScript JavaScript 脚本内容类型
	JavaScript = "application/javascript"
	// Json JSON 数据类型
	Json = "application/json"
	// Html HTML 文本类型
	Html = "text/html"
	// Xml XML 数据类型
	Xml = "application/xml"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

type Body interface {
	// GetData 返回当前请求体的原始数据内容(字符串形式)
	// 通常用于调试或日志,不一定是最终发送的格式
	GetData() string
	// GetContentType 返回该请求体对应的 Content-Type 值
	// 用于在 HTTP 请求头中设置正确的 MIME 类型
	GetContentType() string
	// Encode 将当前请求体编码为 io.Reader,供 HTTP 请求作为 body 使用
	// 每个实现通常根据自身所需格式返回相应的 Reader
	Encode() io.Reader
}

Body 定义了可作为 HTTP 请求体的统一接口 所有请求体类型(如 Raw、Url、Form 等)都应实现该接口 通过该接口,外部在构建请求时可以以统一方式获取内容、类型与最终编码形式

type Form

type Form struct {
	// contains filtered or unexported fields
}

Form 用于构建 multipart/form-data 类型的表单请求体 支持添加普通字段与文件字段,并最终编码为可用于 HTTP 请求的 io.Reader

func NewFormData

func NewFormData() *Form

NewFormData 创建一个新的 Form 实例 返回的 Form 可用于设置字段、文件内容并生成 multipart/form-data 请求体

func (*Form) Encode

func (this *Form) Encode() io.Reader

Encode 关闭 multipart.Writer 并返回完整的 multipart/form-data 编码内容 返回的 io.Reader 可直接作为 HTTP 请求 body 注意:调用 Encode() 后将无法再次向 Form 中添加字段

func (*Form) GetContentType

func (this *Form) GetContentType() string

GetContentType 返回该表单对应的 Content-Type,包含 boundary

func (*Form) GetData

func (this *Form) GetData() string

GetData 返回当前 Form 的 multipart 数据文本形式

func (*Form) SetBoundary

func (this *Form) SetBoundary(boundary string) *Form

SetBoundary 手动设置 multipart/form-data 边界值 一般情况下不需要手动设置边界,除非对外兼容性有特殊要求

func (*Form) SetData

func (this *Form) SetData(name, value string) *Form

SetData 向表单中添加一个普通字段(name=value) 参数 name 表示字段名,value 表示字段内容 支持多次链式调用

func (*Form) SetFile

func (this *Form) SetFile(name, file string) *Form

SetFile 向表单中添加一个文件字段 参数 name 为表单字段名,file 为本地文件路径 支持多次链式调用

type Raw

type Raw struct {
	// contains filtered or unexported fields
}

Raw 用于构建原始字符串类型的请求体(raw body) 可用于发送 JSON、XML、纯文本或任意自定义格式的数据

func NewRawData

func NewRawData() *Raw

NewRawData 创建一个空的 Raw 请求体对象 通过 SetData 设置内容和格式,最终 Encode 生成 io.Reader

func (*Raw) Encode

func (this *Raw) Encode() io.Reader

Encode 将当前数据包装为 io.Reader,用于构建 HTTP 请求的 body

func (*Raw) GetContentType

func (this *Raw) GetContentType() string

GetContentType 返回当前 Raw 数据的 Content-Type 字段

func (*Raw) GetData

func (this *Raw) GetData() string

GetData 返回当前的原始字符串内容

func (*Raw) SetData

func (this *Raw) SetData(data, format string)

SetData 设置原始数据内容以及对应的 MIME 格式(Content-Type) 参数 data 为内容,format 为 MIME 类型(例如 Json、Xml 等)

type Url

type Url struct {
	// contains filtered or unexported fields
}

Url 用于构建 application/x-www-form-urlencoded 请求体 通过内部维护的 url.Values 实现键值对表单数据的编码

func NewUrlEncode

func NewUrlEncode() *Url

NewUrlEncode 创建一个新的 Url 实例,用于构建 URL-encoded 表单数据 返回的 Url 可继续通过 SetData 添加字段,最终 Encode 为 io.Reader

func (*Url) Encode

func (this *Url) Encode() io.Reader

Encode 返回编码后的表单内容,用于作为 HTTP 请求的 body

func (*Url) GetContentType

func (this *Url) GetContentType() string

GetContentType 返回该类型的 Content-Type

func (*Url) GetData

func (this *Url) GetData() string

GetData 返回当前表单的 URL-encoded 文本形式

func (*Url) SetData

func (this *Url) SetData(name, value string) *Url

SetData 向表单中添加一个字段(name=value) 若同一个 name 被多次添加,将作为多值字段存在 支持多次链式调用

Jump to

Keyboard shortcuts

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