https

package
v0.0.0-...-54dc0dc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package https 提供简洁的 HTTP 请求封装。

所有函数的约定:

  • timeout 单位毫秒,传 0 表示不限制超时;
  • 请求失败(网络错误、构造失败)时返回 code=0、data=nil,不返回 error;
  • code 为服务器返回的 HTTP 状态码(如 200、404)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(url string, timeout int) (code int, data []byte)

Delete 发送 DELETE 请求。

func Download

func Download(url, savePath string, timeout int) bool

Download 下载文件到指定路径,成功返回 true。 目标目录不存在时自动创建;先写临时文件再改名,避免残留半成品。

ok := https.Download("https://xx.com/a.png", "/sdcard/a.png", 30000)

func Get

func Get(url string, timeout int) (code int, data []byte)

Get 发送 GET 请求。

func GetString

func GetString(url string, timeout int) string

GetString 发送 GET 请求并把响应体作为字符串返回,失败返回空串。

func GetWithHeaders

func GetWithHeaders(url string, headers map[string]string, timeout int) (code int, data []byte)

GetWithHeaders 发送带自定义请求头的 GET 请求。

func Head(url string, timeout int) (code int)

Head 发送 HEAD 请求,仅返回状态码(无响应体)。

func Patch

func Patch(url string, body []byte, contentType string, timeout int) (code int, data []byte)

Patch 发送 PATCH 请求。

func Post

func Post(url string, data []byte, headers map[string]string, timeout int) (code int, resp []byte)

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

func PostForm(reqURL string, form map[string]string, timeout int) (code int, data []byte)

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

func PostJSON(url string, jsonBody string, timeout int) (code int, data []byte)

PostJSON 发送 JSON 格式的 POST 请求。

code, data := https.PostJSON("https://api.xx.com/login", `{"user":"a","pwd":"b"}`, 5000)

func PostMultipart

func PostMultipart(url string, fileName string, fileData []byte) (code int, data []byte)

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 Put

func Put(url string, body []byte, contentType string, timeout int) (code int, data []byte)

Put 发送 PUT 请求。

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL