Documentation
¶
Overview ¶
Package https 提供简洁的 HTTP 请求封装。
所有函数的约定:
- timeout 单位毫秒,传 0 表示不限制超时;
- 请求失败(网络错误、构造失败)时返回 code=0、data=nil,不返回 error;
- code 为服务器返回的 HTTP 状态码(如 200、404)。
Index ¶
- func Delete(url string, timeout int) (code int, data []byte)
- func Download(url, savePath string, timeout int) bool
- func Get(url string, timeout int) (code int, data []byte)
- func GetString(url string, timeout int) string
- func GetWithHeaders(url string, headers map[string]string, timeout int) (code int, data []byte)
- func Head(url string, timeout int) (code int)
- func Patch(url string, body []byte, contentType string, timeout int) (code int, data []byte)
- func Post(url string, data []byte, headers map[string]string, timeout int) (code int, resp []byte)
- func PostForm(reqURL string, form map[string]string, timeout int) (code int, data []byte)
- func PostJSON(url string, jsonBody string, timeout int) (code int, data []byte)
- func PostMultipart(url string, fileName string, fileData []byte) (code int, data []byte)
- func PostMultipartV(url, fieldName, fileName string, fileData []byte, ...) (code int, data []byte)
- func PostWithHeaders(url string, headers map[string]string, body []byte, timeout int) (code int, data []byte)
- func Put(url string, body []byte, contentType string, timeout int) (code int, data []byte)
- func Request(method, url string, headers map[string]string, body []byte, timeout int) (code int, data []byte)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Download ¶
Download 下载文件到指定路径,成功返回 true。 目标目录不存在时自动创建;先写临时文件再改名,避免残留半成品。
ok := https.Download("https://xx.com/a.png", "/sdcard/a.png", 30000)
func GetWithHeaders ¶
GetWithHeaders 发送带自定义请求头的 GET 请求。
func Post ¶
Post 发送 POST 请求并返回响应状态码和数据。支持自定义请求头和请求体, 适用于发送 JSON、XML 等格式的数据。
headers 为 nil 或未设置 Content-Type 时,默认使用 application/json。
code, data := https.Post("https://example.com/api/user", jsonData, map[string]string{
"Authorization": "Bearer your-token",
}, 10000)
func PostForm ¶
PostForm 发送表单格式(application/x-www-form-urlencoded)的 POST 请求。
code, data := https.PostForm("https://api.xx.com/login", map[string]string{
"user": "a",
"pwd": "b",
}, 5000)
func PostJSON ¶
PostJSON 发送 JSON 格式的 POST 请求。
code, data := https.PostJSON("https://api.xx.com/login", `{"user":"a","pwd":"b"}`, 5000)
func PostMultipart ¶
PostMultipart 以 multipart/form-data 上传单个文件(字段名固定为 "file")。
func PostMultipartV ¶
func PostMultipartV(url, fieldName, fileName string, fileData []byte, extraFields map[string]string, timeout int) (code int, data []byte)
PostMultipartV 以 multipart/form-data 上传文件,可自定义文件字段名并附带额外表单字段。
code, data := https.PostMultipartV("https://api.xx.com/upload", "image", "shot.png",
imgData, map[string]string{"token": "xxx"}, 30000)
func PostWithHeaders ¶
func PostWithHeaders(url string, headers map[string]string, body []byte, timeout int) (code int, data []byte)
PostWithHeaders 发送带自定义请求头的 POST 请求。
func Request ¶
func Request(method, url string, headers map[string]string, body []byte, timeout int) (code int, data []byte)
Request 发送自定义请求,method 如 "GET"、"POST"、"PUT",headers/body 可为 nil。
code, data := https.Request("PUT", "https://api.xx.com/v1/item", map[string]string{
"Authorization": "Bearer token",
"Content-Type": "application/json",
}, []byte(`{"id":1}`), 5000)
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.