Documentation
¶
Overview ¶
Package network 提供 HTTP 网络请求工具。
主要功能:
- HTTP GET/POST/PUT 请求
- 表单提交(application/x-www-form-urlencoded)
- JSON 请求和响应处理
使用场景:
- 第三方 API 调用
- 推送通知(华为、Vivo、OPPO、小米等)
- OAuth 登录(GitHub、Gitee 等)
- Webhook 回调
基本用法:
import "github.com/L8CHAT/l8chat-util/network"
// GET 请求
resp, err := network.Get("https://api.example.com/users", map[string]string{
"page": "1",
"size": "20",
}, map[string]string{
"Authorization": "Bearer token", })
// POST JSON 请求 body := []byte(`{"name":"Alice","age":30}`)
resp, err := network.Post("https://api.example.com/users", body, map[string]string{
"Content-Type": "application/json",
})
// POST 表单请求
resultMap, err := network.PostForWWWForm("https://api.example.com/login", map[string]string{
"username": "alice",
"password": "secret",
}, map[string]string{
"Content-Type": "application/x-www-form-urlencoded", })
注意事项:
- 依赖 github.com/sendgrid/rest 库
- 依赖 github.com/L8CHAT/l8chat-util 用于 JSON 处理
- 所有函数都返回错误,需要妥善处理
- 表单提交会自动设置 Content-Type
Index ¶
- func Get(url string, queryParams map[string]string, headers map[string]string) (resp *rest.Response, err error)
- func GetJson(url string, queryParams map[string]string, headers map[string]string) (byts []byte, err error)
- func Post(url string, body []byte, headers map[string]string) (resp *rest.Response, err error)
- func PostForQueryParam(url string, queryParams map[string]string, headers map[string]string) (resp *rest.Response, err error)
- func PostForWWWForm(urlStr string, params map[string]string, headers map[string]string) (map[string]interface{}, error)
- func PostForWWWFormForAll(urlStr string, bodyData io.Reader, headers map[string]string) ([]byte, error)
- func PostForWWWFormForBytres(urlStr string, params map[string]string, headers map[string]string) ([]byte, error)
- func PostForWWWFormReXML(urlStr string, params map[string]string, headers map[string]string) ([]byte, error)
- func Put(url string, body []byte, headers map[string]string) (resp *rest.Response, err error)
- func RequestBoy(url string, body []byte, headers map[string]string, method rest.Method) (resp *rest.Response, err error)
- func RequestBoyForQueryParam(url string, queryParams map[string]string, headers map[string]string, ...) (resp *rest.Response, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PostForQueryParam ¶
func PostForWWWForm ¶
func PostForWWWFormForAll ¶
func PostForWWWFormForBytres ¶
func PostForWWWFormReXML ¶
func RequestBoy ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.