ghttp

package module
v1.0.1-0...-21131e7 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: Apache-2.0 Imports: 19 Imported by: 10

README

Ghttp

Build Status Go Report Card GoDoc

install

$ go get -u github.com/swxctx/ghttp

序言

Gohttp主要是对golang-http请求的一些简要封装,使用ghttp可以很方便的对http的请求、响应等做操作。目前具备如下几种功能:

  • 支持Http常用请求
  • 支持query及body参数
  • 参数支持interface结构
  • 支持灵活定制参数
  • 支持灵活添加Http请求头
  • 支持cookie
  • 支持超时设置
  • 支持响应数据转换为Struct
  • 支持代理设置
    具体使用方法可以参照Test文件,里面列举了几种使用方法及情况。

GET

Ghttp默认为Get请求,基本请求如下所示:

res, err := ghttp.Request{
	Url:   "http://127.0.0.1:8080",
}.Do()

POST

Ghttp使用POST请求与golang请求一致,基本请求如下所示:

res, err := ghttp.Request{
	Method: "POST",
	Url:   "http://127.0.0.1:8080",
}.Do()

application/x-www-form-urlencoded

parmas := url.Values{}
parmas.Set("idcard", "123")
parmas.Set("name", "123")
req := ghttp.Request{
	Method:    "POST",
	Url:       "http://www.baidu.com",
	ShowDebug: true,
	Body:      parmas,
}
req.AddHeader("Content-Type", "application/x-www-form-urlencoded")
res, err := req.Do()
if err != nil {
	log.Panicln(err)
}
log.Println(res)

请求参数

Ghttp参数支持interface类型,允许直接将Struct作为参数赋值传递,同时支持tags配置指定参数名称以及忽略参数空值,使用如下所示。

使用Struct作为参数

直接使用struct作为参数时,默认元素小写后作为url参数,下面例子请求后生成url如request所示:

// request-> Get http://127.0.0.1:8080?name=xc&password=xc
type User struct {
	Name     string
	Password string
}
user := User{
	Name:     "xc",
	Password: "xc",
}
res, err := ghttp.Request{
	Url:   "http://127.0.0.1:8080",
	Query: user,
}.Do()
Tags配置参数

Ghttp支持将参数在结构体中配置为指定名称,关键字"-"表示此参数忽略不会拼接到url中,"omitempty"关键词表示该字段为空时不做拼接,可参考下面例子生成的url。

// Get http://127.0.0.1:8080?name=xc
type User struct {
	Name     string `json:"name"`
	Password string `json:"-"`
	Sex      string `json:"sex,omitempty"`
}
user := User{
	Name:     "xc",
	Password: "xc",
	Sex:      "",
}
res, err := ghttp.Request{
	Url:   "http://127.0.0.1:8080",
	Query: user,
}.Do()
结构体嵌套

Ghttp支持结构体嵌套的方式拼接参数,这应该也是最为常见的一种方式,例子如下:

Header

Ghttp支持Head添加处理,如下所示:

type User struct {
	Name     string
	Password string
}
user := User{
	Name:     "xc",
	Password: "xc",
}
req := &ghttp.Request{
	Method:      "POST",
	Url:         "http://127.0.0.1:8080",
	Query:       user,
	ContentType: "application/json",
}
req.AddHeader("X-Custom", "haha")
res, err := req.Do()
res, err := ghttp.Request{
	Url:     "http://www.baidu.com",
	Timeout: 100 * time.Millisecond,
}.Do()

响应数据结构转换-Struct

type User struct {
	Name     string `json:"name"`
	Password string `json:"password"`
	Sex      string `json:"sex"`
}
var user User
res, err := ghttp.Request{
	Url:     "http://127.0.0.1:8080",
	Timeout: 100 * time.Millisecond,
}.Do()
if err != nil {
	log.Panicln(err)
}
log.Println(res.Body.FromToJson(&user))

Proxy

res, err := ghttp.Request{
	Url:     "http://127.0.0.1:8080",
	Timeout: 100 * time.Millisecond,
	Proxy:   "http://127.0.0.1:8088",
}.Do()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultDialer
	DefaultDialer = &net.Dialer{Timeout: 2000 * time.Millisecond}
	// DefaultTransport
	DefaultTransport http.RoundTripper = &http.Transport{Dial: DefaultDialer.Dial, Proxy: http.ProxyFromEnvironment}
	// DefaultClient
	DefaultClient = &http.Client{Transport: DefaultTransport}
)

Functions

func Deflate

func Deflate() *compression

Deflate

func GetTlsConfig

func GetTlsConfig(cerPath, keyPath string) *tls.Config

GetTlsConfig 生成tls config

func Gzip

func Gzip() *compression

Gzip

func SetConnectTimeout

func SetConnectTimeout(duration time.Duration)

SetConnectTimeout

func Zlib

func Zlib() *compression

Zlib

Types

type Body

type Body struct {
	// contains filtered or unexported fields
}

Body

func (*Body) Close

func (b *Body) Close() error

Close

func (*Body) FromToJson

func (b *Body) FromToJson(o interface{}) error

FromToJson

func (*Body) FromToString

func (b *Body) FromToString() (string, error)

FromToString

func (*Body) Read

func (b *Body) Read(p []byte) (int, error)

Read

type Error

type Error struct {
	Err error
	// contains filtered or unexported fields
}

Error

func (*Error) Error

func (e *Error) Error() string

Error

func (*Error) Timeout

func (e *Error) Timeout() bool

Timeout

type Request

type Request struct {
	Method string
	Url    string
	Body   interface{}
	Query  interface{}

	Timeout           time.Duration
	ContentType       string
	XForwardedFor     string
	Accept            string
	UserAgent         string
	Host              string
	Insecure          bool
	TlsConfig         *tls.Config
	MaxRedirects      int
	RedirectHeaders   bool
	Proxy             string
	Compression       *compression
	BasicAuthUsername string
	BasicAuthPassword string
	CookieJar         http.CookieJar
	ShowDebug         bool
	OnBeforeRequest   func(goxhttp *Request, httpreq *http.Request)
	// contains filtered or unexported fields
}

Request ghttp request lib

func (*Request) AddCookie

func (r *Request) AddCookie(c *http.Cookie)

AddCookie

func (*Request) AddHeader

func (r *Request) AddHeader(key string, value string)

AddHeader

func (Request) Do

func (r Request) Do() (*Response, error)

Do Initiate a request

func (Request) NewRequest

func (r Request) NewRequest() (*http.Request, error)

NewRequest new request before do()

func (Request) WithCookie

func (r Request) WithCookie(c *http.Cookie) Request

WithCookie

func (Request) WithHeader

func (r Request) WithHeader(key string, value string) Request

WithHeader

type Response

type Response struct {
	*http.Response
	Url  string
	Body *Body
	// contains filtered or unexported fields
}

Response ghttp response

func (Response) CancelRequest

func (r Response) CancelRequest()

CancelRequest cancel like postman

Jump to

Keyboard shortcuts

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