# c2 **Repository Path**: tuboyou/c2 ## Basic Information - **Project Name**: c2 - **Description**: 使用golang 发起http请求 - **Primary Language**: Go - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-12-31 - **Last Updated**: 2025-08-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # C2 HTTP 请求库 C2 是一个功能强大的 HTTP 请求库,支持 GET、POST 等多种请求方式,包含请求构建、响应解析、结果校验等特性。 ## 主要功能 ### 请求构建 - 支持 GET、POST、Multipart 等多种请求方式 - 支持设置请求头、请求参数、请求体 - 支持 JSON、Form、Text 等多种请求体格式 - 支持文件上传 - 支持设置超时时间和重试次数 ### 响应处理 - 自动解析 JSON 响应 - 支持响应结果绑定到结构体 - 支持响应结果校验 - 支持将响应结果保存到文件 - 支持生成 curl 命令 ### 辅助工具 - 提供丰富的打印方法,方便调试 - 支持全局配置(超时时间、重试次数等) - 支持设置全局请求头 ## 快速开始 ```go // 创建 GET 请求 req := c2.Get("test", "https://example.com", "/api/v1/test") // 设置请求参数 req.SetParam("id", 123) // 执行请求 resp := req.Run() // 打印响应结果 resp.PrintStruct() ``` ## u2 辅助工具 u2 包提供了一些常用的辅助函数: ### 文件操作 - `ExistFile(path string) bool` 检查文件是否存在 - `RemoveFile(path string) error` 删除文件 - `AppendFile(path, content string) error` 追加内容到文件 ### 类型转换 - `I2S(v interface{}) string` 将任意类型转换为字符串 - `Struct2JSON(v interface{}, pretty bool) (string, error)` 将结构体转换为 JSON 字符串 - `Json2Interface(data []byte, v interface{}) error` 将 JSON 字符串解析到结构体 ### 其他工具 - `Now() string` 获取当前时间字符串 - `RandStr(n int) string` 生成随机字符串 - `Md5(str string) string` 计算字符串的 MD5 值 ## 示例 ### 创建 POST 请求 ```go req := c2.Post("test", "https://example.com", "/api/v1/test") req.SetBodyJson(map[string]interface{}{ "name": "John", "age": 30, }) resp := req.Run() ``` ### 文件上传 ```go req := c2.PostMultipart("test", "https://example.com", "/api/v1/upload") req.SetFile("file", "/path/to/file") resp := req.Run() ``` ### 响应结果校验 ```go resp.Check(200, map[string]string{ "Content-Type": "application/json", }, map[string]interface{}{ "name": "John", "age": 30, }) ``` ## 配置 ### 全局配置 ```go // 设置全局重试次数 c2.SetRetry(3) // 设置结果保存路径 c2.SetResultPath("/path/to/result", true) // 设置全局请求头 c2.SetGlobalHeader("Authorization", "Bearer token") ``` ### 请求配置 ```go // 设置超时时间 req.SetTimeout(10000) // 设置重试次数 req.SetRetry(5) ```