@@ -8,34 +8,115 @@ import (
88 "strings"
99)
1010
11+ // session 是一个全局默认会话实例,用于包级别的便捷方法
12+ // session is a global default session instance used for package-level convenience methods
1113var session = New ()
1214
13- // Get send get request
15+ // Get 发送 GET 请求
16+ // 这是一个便捷方法,完全兼容 net/http 包的使用方式
17+ // Get sends a GET request
18+ // This is a convenience method, fully compatible with the net/http package usage
19+ //
20+ // 参数 / Parameters:
21+ // - url: 请求的URL地址 / The URL to request
22+ //
23+ // 返回值 / Returns:
24+ // - *http.Response: HTTP响应对象,注意:必须手动关闭 resp.Body / HTTP response object, note: resp.Body must be closed manually
25+ // - error: 请求过程中的错误 / Error during the request
26+ //
27+ // 示例 / Example:
28+ //
29+ // resp, err := requests.Get("https://api.example.com/users")
30+ // if err != nil {
31+ // log.Fatal(err)
32+ // }
33+ // defer resp.Body.Close()
1434func Get (url string ) (* http.Response , error ) {
1535 return session .Do (context .Background (), MethodGet , URL (url ))
1636}
1737
18- // Post send post request
38+ // Post 发送 POST 请求
39+ // Post sends a POST request
40+ //
41+ // 参数 / Parameters:
42+ // - url: 请求的URL地址 / The URL to request
43+ // - contentType: 内容类型,例如 "application/json" / Content type, e.g., "application/json"
44+ // - body: 请求体数据 / Request body data
45+ //
46+ // 返回值 / Returns:
47+ // - *http.Response: HTTP响应对象 / HTTP response object
48+ // - error: 请求过程中的错误 / Error during the request
49+ //
50+ // 示例 / Example:
51+ //
52+ // body := strings.NewReader(`{"name": "John"}`)
53+ // resp, err := requests.Post("https://api.example.com/users", "application/json", body)
1954func Post (url string , contentType string , body io.Reader ) (* http.Response , error ) {
2055 return session .Do (context .TODO (), MethodPost , URL (url ), Header ("Content-Type" , contentType ), Body (body ))
2156}
2257
23- // PUT send put request
58+ // Put 发送 PUT 请求
59+ // Put sends a PUT request
60+ //
61+ // 参数 / Parameters:
62+ // - url: 请求的URL地址 / The URL to request
63+ // - contentType: 内容类型 / Content type
64+ // - body: 请求体数据 / Request body data
65+ //
66+ // 返回值 / Returns:
67+ // - *http.Response: HTTP响应对象 / HTTP response object
68+ // - error: 请求过程中的错误 / Error during the request
2469func Put (url , contentType string , body io.Reader ) (* http.Response , error ) {
2570 return session .Do (context .TODO (), Method ("PUT" ), URL (url ), Header ("Content-Type" , contentType ), Body (body ))
2671}
2772
28- // Delete send delete request
73+ // Delete 发送 DELETE 请求
74+ // Delete sends a DELETE request
75+ //
76+ // 参数 / Parameters:
77+ // - url: 请求的URL地址 / The URL to request
78+ // - contentType: 内容类型 / Content type
79+ // - body: 请求体数据 / Request body data
80+ //
81+ // 返回值 / Returns:
82+ // - *http.Response: HTTP响应对象 / HTTP response object
83+ // - error: 请求过程中的错误 / Error during the request
2984func Delete (url , contentType string , body io.Reader ) (* http.Response , error ) {
3085 return session .Do (context .TODO (), Method ("DELETE" ), URL (url ), Header ("Content-Type" , contentType ), Body (body ))
3186}
3287
33- // Head send post request
88+ // Head 发送 HEAD 请求
89+ // Head sends a HEAD request
90+ //
91+ // 参数 / Parameters:
92+ // - url: 请求的URL地址 / The URL to request
93+ //
94+ // 返回值 / Returns:
95+ // - resp: HTTP响应对象 / HTTP response object
96+ // - err: 请求过程中的错误 / Error during the request
3497func Head (url string ) (resp * http.Response , err error ) {
3598 return session .Do (context .Background (), Method ("HEAD" ), URL (url ))
3699}
37100
38- // PostForm send post request, content-type = application/x-www-form-urlencoded
101+ // PostForm 发送表单 POST 请求
102+ // 自动设置 Content-Type 为 application/x-www-form-urlencoded
103+ // PostForm sends a form POST request
104+ // Automatically sets Content-Type to application/x-www-form-urlencoded
105+ //
106+ // 参数 / Parameters:
107+ // - url: 请求的URL地址 / The URL to request
108+ // - data: 表单数据 / Form data
109+ //
110+ // 返回值 / Returns:
111+ // - *http.Response: HTTP响应对象 / HTTP response object
112+ // - error: 请求过程中的错误 / Error during the request
113+ //
114+ // 示例 / Example:
115+ //
116+ // data := url.Values{}
117+ // data.Set("username", "john")
118+ // data.Set("password", "secret")
119+ // resp, err := requests.PostForm("https://api.example.com/login", data)
39120func PostForm (url string , data url.Values ) (* http.Response , error ) {
40121 return session .Do (context .TODO (), MethodPost , URL (url ), Header ("Content-Type" , "application/x-www-form-urlencoded" ),
41122 Body (strings .NewReader (data .Encode ())),
0 commit comments