network

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: MIT Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(url string, queryParams map[string]string, headers map[string]string) (resp *rest.Response, err error)

func GetJson

func GetJson(url string, queryParams map[string]string, headers map[string]string) (byts []byte, err error)

func Post

func Post(url string, body []byte, headers map[string]string) (resp *rest.Response, err error)

func PostForQueryParam

func PostForQueryParam(url string, queryParams map[string]string, headers map[string]string) (resp *rest.Response, err error)

func PostForWWWForm

func PostForWWWForm(urlStr string, params map[string]string, headers map[string]string) (map[string]interface{}, error)

func PostForWWWFormForAll

func PostForWWWFormForAll(urlStr string, bodyData io.Reader, headers map[string]string) ([]byte, error)

func PostForWWWFormForBytres

func PostForWWWFormForBytres(urlStr string, params map[string]string, headers map[string]string) ([]byte, error)

func PostForWWWFormReXML

func PostForWWWFormReXML(urlStr string, params map[string]string, headers map[string]string) ([]byte, error)

func Put

func Put(url string, body []byte, headers map[string]string) (resp *rest.Response, err error)

func RequestBoy

func RequestBoy(url string, body []byte, headers map[string]string, method rest.Method) (resp *rest.Response, err error)

func RequestBoyForQueryParam

func RequestBoyForQueryParam(url string, queryParams map[string]string, headers map[string]string, method rest.Method) (resp *rest.Response, err error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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