httphelper

package
v0.1.24 Latest Latest
Warning

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

Go to latest
Published: May 23, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DEBUG_DISABLED = 2
	DEBUG_NORMAL   = 3
	DEBUG_DETAIL   = 4
)

Variables

View Source
var (
	// Log 日志输出函数,可修改
	Log = func(v ...interface{}) { fmt.Println(v...) }
)
View Source
var NilValueErr = errors.New("nil value")

NilValueErr 空值错误

Functions

func PathEscape

func PathEscape(param map[string]string) string

func PathEscapeEncode added in v0.1.8

func PathEscapeEncode(v url.Values) string

func UrlPathEscape

func UrlPathEscape(url string) string

func UrlToMap

func UrlToMap(url string) map[string]string

Types

type Helper

type Helper interface {
	// AddQuery 添加Query参数
	AddQuery(k string, v string) Helper

	// AddQueryMap 通过map来添加Query参数
	AddQueryMap(m map[string]string) Helper

	// AddPathEscapeQuery 添加Query参数,并进行PathEscape转义
	AddPathEscapeQuery(k string, v string) Helper

	AddPathEscapeQueryMap(m map[string]string) Helper
	// AddHeader 添加HTTP header
	AddHeader(k string, v string) Helper

	// SetHeader 设置HTTP header,会覆盖同名的header
	SetHeader(k string, v string) Helper

	// SetTransport 设置RoundTripper,可用于添加中间件
	SetTransport(tripper http.RoundTripper) Helper

	// AddCookies 设置完整cookies
	AddCookies(c []*http.Cookie) Helper

	// AddSimpleCookies 设置只有简单name和value的cookies
	AddSimpleCookies(c map[string]string) Helper

	SetDebug(mode int)

	// Do 发送请求
	Do() Result
}

Helper http Helper接口的提供的方法

func GET

func GET(url string) Helper

GET 创建新的GET请求

func NewRequest

func NewRequest(method string, urlStr string, body io.Reader) Helper

NewRequest 创建新的请求

func PostJSON

func PostJSON(url string, body interface{}) Helper

PostJSON 创建application/json的POST请求

func PostUrlEncode

func PostUrlEncode(url string, values url.Values) Helper

PostUrlEncode 创建application/x-www-form-urlencoded的POST请求

type Result

type Result interface {
	// BaseResult 返回Http请求的基本结果,包含Status和Body
	BaseResult() *baseResult

	// StdCheck 检查HTTP响应码和Body中的Code
	StdCheck() Result

	// Bind 将返回值存储到Object中
	Bind(object interface{}, path ...interface{}) error

	// Get 通过json path获取子结果
	Get(path ...interface{}) Result

	// Error 返回出现的错误
	Error() error

	// Jsoniter 返回body的json迭代器
	Jsoniter() jsoniter.Any

	// UnmarshalFromBody json UnmarshalFromBody
	UnmarshalFromBody(v interface{}) error
}

Result Http请求的结果

Jump to

Keyboard shortcuts

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