Documentation
¶
Overview ¶
Package c2 提供HTTP请求和响应处理功能,支持GET、POST等多种请求方式,包含请求构建、响应解析、结果校验等特性。
主要功能:
- 支持多种HTTP请求方法
- 支持请求参数、请求头、请求体的设置
- 支持JSON、Form、Text等多种请求体格式
- 支持文件上传
- 提供丰富的响应处理功能
- 支持全局配置管理
Package c2 提供HTTP请求参数文档相关的功能,用于描述和记录API请求参数信息。
Index ¶
- func SendQiWei(key, title, content string)
- func SetGlobalHeader(name, value string)
- func SetResultPath(p string, isRemoveOld bool)
- func SetRetry(r int)
- func SetXfrom(xfrom string)
- type MultipartRequest
- func (t *MultipartRequest) Run() Response
- func (t *MultipartRequest) SetBody(Name, Value string)
- func (t *MultipartRequest) SetFile(Name, Value string)
- func (t *MultipartRequest) SetHeader(Name, Value string)
- func (t *MultipartRequest) SetMeta(Name, Value string)
- func (t *MultipartRequest) SetParam(Name, Value string)
- func (t *MultipartRequest) SetRetry(r int)
- func (t *MultipartRequest) SetTimeout(timeout int)
- func (t *MultipartRequest) String() string
- type ParamsDoc
- type Request
- func (t *Request) Body2String() string
- func (t *Request) Curl() string
- func (t *Request) GetBodyJson() interface{}
- func (t *Request) GetBodyJsonParamString(key string) string
- func (t *Request) GetBodyType() config.BodyChoose
- func (t *Request) GetBodyVal(key string) string
- func (t *Request) GetbodyForms() map[string]string
- func (t *Request) Run() Response
- func (t *Request) SetBody(body string) *ParamsDoc
- func (t *Request) SetBodyForm(Name string, Value interface{}) *ParamsDoc
- func (t *Request) SetBodyForms(body map[string]string) *ParamsDoc
- func (t *Request) SetBodyJson(body interface{}) *ParamsDoc
- func (t *Request) SetBodyJsonList(value interface{}) *ParamsDoc
- func (t *Request) SetBodyJsonMap(key string, value interface{}) *ParamsDoc
- func (t *Request) SetDescription(description string) *Request
- func (t *Request) SetExpectBody(name string, value interface{})
- func (t *Request) SetHeader(Name, Value string)
- func (t *Request) SetMeta(Name, Value string)
- func (t *Request) SetParam(Name string, Value interface{}) *ParamsDoc
- func (t *Request) SetRetry(r int)
- func (t *Request) SetTimeout(timeout int)
- func (t *Request) String() string
- type Response
- func (r Response) Bind(struct_data interface{}) error
- func (r Response) Check(code int, header map[string]string, body interface{}) string
- func (r Response) Curl()
- func (r Response) Curl2File(p string)
- func (t Response) GetExpectBody(name string) interface{}
- func (t Response) GetExpectBodyOnce() interface{}
- func (r Response) GetResult() interface{}
- func (r Response) PrintDetail()
- func (r Response) PrintInfo()
- func (r Response) PrintRequestInfo(showHeader bool)
- func (r Response) PrintResponseInfo(showStatusCode, showHeader bool)
- func (r Response) PrintStruct()
- func (r Response) PrintStructInfo()
- func (r Response) PrintStructNoResult()
- func (r Response) Result2File(p string, hasRequest, hasHeader bool)
- func (r Response) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetGlobalHeader ¶
func SetGlobalHeader(name, value string)
SetGlobalHeader 设置全局请求头
参数:
name - 头名称 value - 头值
func SetResultPath ¶
SetResultPath 设置结果保存路径
参数:
p - 文件路径 isRemoveOld - 是否立即删除原有文件
注意:
- 如果isRemoveOld为true,会立即删除指定路径的文件
- 如果启用了调试模式,会打印设置日志
Types ¶
type MultipartRequest ¶
type MultipartRequest struct { Name string `json:"name"` Host string `json:"host"` Path string `json:"path"` Params map[string]string `json:"param"` Headers map[string]string `json:"headers"` Files map[string]string `json:"files"` Body map[string]string `json:"body"` Meta map[string]string `json:"meta"` //元数据,用于存储一些信息 Timeout int `json:"timeout"` //单位:毫秒 Error error `json:"error"` //错误信息 Retry int `json:"retry" default:"0"` }
MultipartRequest 表示一个multipart/form-data格式的HTTP请求 字段说明:
Name - 请求名称 Host - 请求主机地址 Path - 请求路径 Params - 请求参数 Headers - 请求头 Files - 要上传的文件 Body - 请求体 Meta - 元数据,用于存储一些附加信息 Timeout - 请求超时时间,单位为毫秒 Error - 错误信息 Retry - 重试次数
func PostMultipart ¶
func PostMultipart(name, host, path string) *MultipartRequest
PostMultipart 创建一个新的multipart/form-data格式的POST请求
参数:
name - 请求名称 host - 请求主机 path - 请求路径
返回值:
*MultipartRequest - 新创建的MultipartRequest对象
注意:
- 默认会添加xfrom请求头
- 默认超时时间为5000毫秒
- 支持文件上传
func (*MultipartRequest) Run ¶
func (t *MultipartRequest) Run() Response
Run 执行multipart/form-data格式的HTTP请求
返回值:
Response - 包含响应结果的结构体
注意:
- 会自动处理重试逻辑
- 会将结果保存到指定路径(如果设置了结果保存路径)
- 会合并全局请求头
- 如果启用了调试模式,会打印请求和重试日志
func (*MultipartRequest) SetBody ¶
func (t *MultipartRequest) SetBody(Name, Value string)
SetBody 设置multipart请求体
参数:
Name - 字段名称 Value - 字段值
注意:
- 如果启用了调试模式,会打印设置日志
func (*MultipartRequest) SetFile ¶
func (t *MultipartRequest) SetFile(Name, Value string)
SetFile 设置multipart要上传的文件
参数:
Name - 字段名称 Value - 文件路径
注意:
- 如果文件不存在会返回错误
- 会自动去除路径中的多余引号
- 如果启用了调试模式,会打印设置日志
func (*MultipartRequest) SetHeader ¶
func (t *MultipartRequest) SetHeader(Name, Value string)
SetHeader 设置multipart请求头
参数:
Name - 头名称 Value - 头值
注意:
- 如果启用了调试模式,会打印设置日志
func (*MultipartRequest) SetMeta ¶
func (t *MultipartRequest) SetMeta(Name, Value string)
SetMeta 设置multipart元数据
参数:
Name - 元数据名称 Value - 元数据值
注意:
- 如果启用了调试模式,会打印设置日志
func (*MultipartRequest) SetParam ¶
func (t *MultipartRequest) SetParam(Name, Value string)
SetParam 设置multipart请求参数
参数:
Name - 参数名称 Value - 参数值
注意:
- 如果启用了调试模式,会打印设置日志
func (*MultipartRequest) SetRetry ¶
func (t *MultipartRequest) SetRetry(r int)
SetRetry 设置multipart请求重试次数
参数:
r - 重试次数
注意:
- 如果启用了调试模式,会打印设置日志
func (*MultipartRequest) SetTimeout ¶
func (t *MultipartRequest) SetTimeout(timeout int)
SetTimeout 设置multipart请求超时时间
参数:
timeout - 超时时间,单位为毫秒
注意:
- 如果启用了调试模式,会打印设置日志
func (*MultipartRequest) String ¶
func (t *MultipartRequest) String() string
String 获取multipart请求的字符串表示
返回值:
string - 请求的JSON格式字符串
type ParamsDoc ¶ added in v0.0.4
type ParamsDoc struct { Name string `json:"name"` // 参数名称 Description string `json:"description"` // 参数描述 Type config.ParamType `json:"type"` // 参数类型 Required bool `json:"required"` // 是否必填 Default string `json:"default"` // 默认值 Urlencoded bool `json:"urlencoded"` // 是否为urlencode编码 }
ParamsDoc 表示一个API请求参数的文档信息
字段说明:
Name - 参数名称 Description - 参数描述 Type - 参数类型,使用config.ParamType枚举 Required - 是否必填 Default - 默认值 Urlencoded - 是否为urlencode编码
func NewParamsDoc ¶ added in v0.0.4
NewParamsDoc 创建一个新的ParamsDoc实例
参数:
name - 参数名称
返回值:
*ParamsDoc - 新创建的ParamsDoc指针
func (*ParamsDoc) SetDescription ¶ added in v0.0.4
func (t *ParamsDoc) SetDescription(description string, Type config.ParamType, Required bool, Default string, Urlencoded bool)
SetDescription 设置参数文档的详细信息
参数:
description - 参数描述 Type - 参数类型,使用config.ParamType枚举 Required - 是否必填 Default - 默认值 Urlencoded - 是否为urlencode编码
注意:
- 该方法会一次性设置除Name之外的所有字段
- 参数类型使用config包中定义的ParamType枚举
type Request ¶
type Request struct { Name string `json:"name"` // 请求名称 Description string `json:"description"` // 请求描述 Method string `json:"method"` // 请求方法(GET/POST等) Host string `json:"host"` // 请求主机 Path string `json:"path"` // 请求路径 Params map[string]string `json:"param"` // 请求参数 Headers map[string]string `json:"headers"` // 请求头 Body string `json:"body"` // 请求体(text/plain格式) Meta map[string]string `json:"meta"` // 元数据,用于存储一些附加信息 Timeout int `json:"timeout"` // 超时时间(毫秒) Error error `json:"error"` // 错误信息 Retry int `json:"retry" default:"0"` // 重试次数 // contains filtered or unexported fields }
Request 表示HTTP请求的结构体
func Get ¶
Get 创建一个新的GET请求
参数:
name - 请求名称 host - 请求主机 path - 请求路径
返回值:
*Request - 新创建的Request对象
注意:
- 默认会添加xfrom请求头
- 默认超时时间为5000毫秒
func New ¶
New 创建一个新的HTTP请求
参数:
name - 请求名称 method - 请求方法(GET/POST等) host - 请求主机 path - 请求路径
返回值:
*Request - 新创建的Request对象
注意:
- 默认会添加xfrom请求头
- 默认超时时间为5000毫秒
示例:
req := New("test", "GET", "https://example.com", "/api/v1/test")
func Post ¶
Post 创建一个新的POST请求
参数:
name - 请求名称 host - 请求主机 path - 请求路径
返回值:
*Request - 新创建的Request对象
注意:
- 默认会添加xfrom请求头
- 默认超时时间为5000毫秒
- 不包含multipart/form-data格式
func (*Request) Body2String ¶
Body2String 将请求体转换为字符串
返回值:
string - 请求体字符串
注意:
- 会根据bodyChoose自动转换请求体格式
- 会自动设置对应的Content-Type
func (*Request) Curl ¶ added in v0.0.2
Curl 生成并返回当前请求的curl命令
返回值:
string - 生成的curl命令
注意:
- 会自动包含请求方法、URL、请求头和请求体
- 对于POST请求,会自动添加-d参数
- 对于multipart/form-data请求,会自动添加-F参数
func (*Request) GetBodyJson ¶ added in v0.0.4
func (t *Request) GetBodyJson() interface{}
func (*Request) GetBodyJsonParamString ¶ added in v0.0.4
func (*Request) GetBodyType ¶
func (t *Request) GetBodyType() config.BodyChoose
GetBodyType 获取请求体格式类型
返回值:
int - 请求体格式类型代码
注意:
0:none 1:text/plain 2:application/json 3:application/x-www-form-urlencoded 4:multipart/form-data
func (*Request) GetBodyVal ¶
GetBodyVal 获取application/x-www-form-urlencoded格式请求体的指定字段值
参数:
key - 字段名
返回值:
string - 字段值
注意:
如果请求体不是application/x-www-form-urlencoded格式,返回空字符串
func (*Request) GetbodyForms ¶ added in v0.0.4
GetbodyForms 获取application/x-www-form-urlencoded格式的请求体
返回值:
map[string]string - 键值对形式的请求体
func (*Request) Run ¶
Run 执行HTTP请求
返回值:
Response - 包含响应结果的结构体
注意:
- 会自动处理重试逻辑
- 会将结果保存到指定路径(如果设置了结果保存路径)
- 会合并全局请求头
func (*Request) SetBodyForm ¶ added in v0.0.4
SetBodyForm 分步设置application/x-www-form-urlencoded格式的请求体
参数:
Name - 字段名 Value - 字段值
注意:
- 如果Value为nil,则删除该字段
- 如果Value为time.Time类型,会自动格式化为"2006-01-02 15:04:05"
func (*Request) SetBodyForms ¶ added in v0.0.4
SetBodyForms 设置application/x-www-form-urlencoded格式的请求体
参数:
body - 键值对形式的请求体
注意:
- 会覆盖原有的body
- 会自动设置请求头的content-type为application/x-www-form-urlencoded
func (*Request) SetBodyJson ¶
SetBodyJson 设置JSON格式的请求体
参数:
body - JSON格式的请求体
注意:
- 会覆盖原有的body
- 会自动设置请求头的content-type为application/json
func (*Request) SetBodyJsonList ¶
SetBodyJsonList 分步设置JSON格式的请求体(list类型)
参数:
value - 要添加的列表项
注意:
- 如果body不是list类型,会返回错误
- 会自动设置请求头的content-type为application/json
func (*Request) SetBodyJsonMap ¶
SetBodyJsonMap 分步设置JSON格式的请求体(map类型)
参数:
key - JSON字段名 value - JSON字段值
注意:
- 如果body不是map类型,会返回错误
- 会自动设置请求头的content-type为application/json
func (*Request) SetDescription ¶
SetDescription 设置请求描述
参数:
description - 描述内容
返回值:
*Request - 返回当前Request对象,支持链式调用
func (*Request) SetExpectBody ¶
SetExpectBody 设置期望的响应体结构
参数:
name - 期望结构的名称 value - 期望结构的值
注意:
- 如果value为nil,则设置为空字符串
- 如果value为指针类型,则直接存储指针
func (*Request) SetParam ¶
SetParam 设置请求参数
参数:
Name - 参数名称 Value - 参数值
注意:
- 如果Value为nil,则删除该参数
- 如果Value为time.Time类型,会自动格式化为"2006-01-02 15:04:05"
type Response ¶
func (Response) Bind ¶
Bind 将响应结果绑定到结构体
参数:
struct_data - 要绑定的结构体指针
返回值:
error - 绑定过程中发生的错误
注意:
- struct_data必须是指针类型
- 如果struct_data不是指针,会自动转换为指针
func (Response) Check ¶
Check 校验响应结果
参数:
code - 期望的HTTP状态码 header - 期望的响应头 body - 期望的响应体
返回值:
string - 校验结果,"ok"表示校验通过,否则返回错误信息
func (Response) GetExpectBody ¶
GetExpectBody 获取期望的响应体结构
参数:
name - 期望结构的名称
返回值:
interface{} - 期望结构的值
注意:
如果未找到对应名称的期望结构,返回nil
func (Response) GetExpectBodyOnce ¶
func (t Response) GetExpectBodyOnce() interface{}
GetExpectBodyOnce 获取第一个期望的响应体结构
返回值:
interface{} - 第一个期望结构的值
注意:
如果未设置任何期望结构,返回nil
func (Response) GetResult ¶
func (r Response) GetResult() interface{}
GetResult 获取响应结果
返回值:
interface{} - 响应结果
func (Response) PrintDetail ¶
func (r Response) PrintDetail()
PrintDetail 打印请求和响应详细信息
注意:
- 会调用PrintRequestInfo和PrintResponseInfo方法
- 显示请求头和响应头信息
func (Response) PrintInfo ¶
func (r Response) PrintInfo()
PrintInfo 打印请求和响应基本数据
注意:
- 会调用PrintRequestInfo和PrintResponseInfo方法
- 不显示请求头和响应头信息
func (Response) PrintRequestInfo ¶
PrintRequestInfo 打印请求数据
参数:
showHeader - 是否显示请求头信息
注意:
- 如果请求方法是POST,会自动显示请求头
- 如果请求体不为空,会打印请求体内容
func (Response) PrintResponseInfo ¶
PrintResponseInfo 打印响应数据
参数:
showStatusCode - 是否显示状态码 showHeader - 是否显示响应头
注意:
- 如果状态码大于399,会打印错误信息
- 如果JSON解析失败,会打印原始响应内容
func (Response) PrintStruct ¶
func (r Response) PrintStruct()
PrintStruct 打印结构体含期望结果,不含请求信息
注意:
- 如果响应为空,会打印空响应信息
- 如果JSON解析失败,会打印错误信息
- 会打印响应耗时
func (Response) PrintStructInfo ¶
func (r Response) PrintStructInfo()
PrintStructInfo 打印结构体,含请求信息
注意:
- 会调用PrintRequestInfo和PrintStruct方法
- 不显示请求头信息
func (Response) PrintStructNoResult ¶
func (r Response) PrintStructNoResult()
PrintStructNoResult 打印结构体,不含请求信息和期望结果
注意:
- 如果响应为空,会打印空响应信息
- 如果JSON解析失败,会打印错误信息
- 会打印响应耗时
func (Response) Result2File ¶
Result2File 结果写入文件,需要包含文件名
参数:
p - 文件路径 hasRequest - 是否写入请求信息 hasHeader - 是否写入响应头信息
注意:
- 如果路径以/结尾,会自动生成文件名
- 如果响应为空,会写入空响应信息
- 如果发生错误,会写入错误信息