http

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2020 License: MIT Imports: 7 Imported by: 0

README

HTTP 客户端

封装http 客户端

使用

import (
  "fmt"
  "github.com/go-eyas/toolkit/http"
)

func main() {
  h := http.Header("Authorization", "Bearer xxxxxxxxxxxxxxx").
    UserAgent("your custom user-agent").
    Cookie().
    BaseURL("https://api.github.com")

  res, err := h.Get("/repos/eyasliu/blog/issues", map[string]string{
    "per_page": 1,
  })

  // 获取字符串
  fmt.Printf("print string: %s\n", res.String())
  // 获取字节
  fmt.Printf("print bytes: %v", res.Byte())

  // 绑定结构体
  s := []struct {
		URL   string `json:"url"`
		Title string `json:"title"`
  }{}
  res.JSON(&s)
  fmt.Printf("print Struct: %v", s)

  // 使用代理
  res, err := http.Proxy("http://127.0.0.1:1080").Get("https://www.google.com", map[string]string{
		"hl": "zh-Hans",
  })
  fmt.Printf("google html: %s", res.String())
}

使用指南

请求示例
// get url
http.Get("https://api.github.com", nil)

// 带查询参数
http.Get("https://www.google.com", "hl=zh-Hans") // 查询参数可以是字符串
http.Get("https://www.google.com", map[string]string{
	"hl": "zh-Hans",
}) // 可以是map
http.Get("https://www.google.com", struct{
  HL string `json:"hl"`
}{"zh-Hans"}) // 可以是结构体,使用json key作为查询参数的key

// post 请求
http.Post("https://api.github.com", nil)

// post 带json参数
http.Post("https://api.github.com", `{"hello": "world"}`) // 可以是字符串
http.Post("https://api.github.com", map[string]interface{}{"hello": "world"}) // 可以是map
http.Post("https://api.github.com", struct{
  Hello string `json:"hello"`
}{"world"}) // 可以是结构体,使用json 序列化字符串

// post 带 查询参数,带json参数
http.Query("hl=zh-Hans").Post("https://api.github.com", `{"hello": "world"}`)

// post form表单
http.Type("multipart").Post("https://api.github.com", map[string]interface{}{"hello": "world"})
// post 上传文件,会以表单提交
http.PostFile("https://api.github.com", "./example_file.txt", map[string]interface{}{"hello": "world"})

// post 上传文件,使用file文件流
file, _ := ioutil.ReadFile("./example_file.txt")
file, _ := os.Open("./example_file.txt")
http.PostFile("https://api.github.com", file, map[string]interface{}{"hello": "world"})

// put, 和post完全一致
http.Put("https://api.github.com", nil)

// delete, 和post完全一致
http.Del("https://api.github.com", nil)

// patch, 和post完全一致
http.Patch("https://api.github.com", nil)

// head, 和get完全一致
http.Head("https://api.github.com", nil)

// options, 和get完全一致
http.Options("https://api.github.com", nil)
响应示例
res, err := http.Options("https://api.github.com", nil)

// 错误信息
if err != nil {
  err.Error() // 错误信息
}
res.Err().Error() // 与上面等价

// 响应数据
// 将响应数据转为字符串
var str string = res.String() 

// 将响应数据转为字节
var bt []byte = res.Byte()

// 获取响应状态码
var statusCode = res.Status()

// 获取响应的 header
var http.Header = res.Header()

// 获取响应的 cookies
var []*http.Cookie = res.Cookies()

// 与结构体绑定
type ResTest struct {
  Hello string `json:"hello"`
}
rt := &ResTest{}
res.JSON(rt)

注意:

  • http的响应状态码 >= 400 时会被视为错误,err 值是 fmt.Errorf("http response status code %d", statusCode)
提前设置通用项
h := http.Header("Authorization", "Bearer xxxxxxxxxxxxxxx"). // 设置header
    UserAgent("your custom user-agent"). // 设置 useragent
    Timeout(10 * time.Second). // 设置请求超时时间
    Query("lang=zh_ch"). // 设置查询参数
    Proxy("http://127.0.0.1:1080") // 设置代理

h.Get("xxxx", nil)
中间件支持

可以增加请求中间件和响应中间件,用于在请求或响应中改变内部操作

http.UseRequest(func(req *http.Request) *http.Request {
    fmt.Printf("http 发送 %s %s\n", req.SuperAgent.Method, req.SuperAgent.Url)
    return req
}).UseResponse(func(req *http.Request, res *http.Response) *http.Response {
    fmt.Printf("http 接收 %s %s\n", req.SuperAgent.Method, req.SuperAgent.Url)
    return res
})
代理设置

默认会获取环境变量 http_proxy 的值使用代理,但是可以手动指定

http.Proxy("http://127.0.0.1:1080").Get("https://www.google.com", map[string]string{
	"hl": "zh-Hans",
})

// 临时取消代理
http.Proxy("").Get("https://www.google.com", map[string]string{
	"hl": "zh-Hans",
})
提交方式

也就是 Type(t string) 函数支持的值

"text/html" uses "html"
"application/json" uses "json"
"application/xml" uses "xml"
"text/plain" uses "text"
"application/x-www-form-urlencoded" uses "urlencoded", "form" or "form-data"

如果是文件上传,则应该设置为 multipart

godoc

API 文档

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Request

type Request struct {
	SuperAgent *gorequest.SuperAgent
	// contains filtered or unexported fields
}

Request 请求结构

func BaseURL

func BaseURL(url string) Request

BaseURL 设置url前缀

func Cookie(c *http.Cookie) Request

Cookie 设置请求 Cookie

func Header(key, val string) Request

Header 设置请求 Header

func New

func New() Request

New 新建请求对象,默认数据类型 json

func Proxy

func Proxy(url string) Request

Proxy 设置请求代理

func Query

func Query(query interface{}) Request

Query 设置请求代理

func Timeout

func Timeout(timeout time.Duration) Request

Timeout 设置请求代理

func Type

func Type(name string) Request

Type 请求提交方式,默认json

func UseRequest

func UseRequest(mdl requestMiddlewareHandler) Request

UseRequest 增加请求中间件

func UseResponse

func UseResponse(mdl responseMidlewareHandler) Request

UseResponse 增加响应中间件

func UserAgent

func UserAgent(name string) Request

UserAgent 设置请求 user-agent,默认是 chrome 75.0

func (Request) BaseURL

func (r Request) BaseURL(url string) Request

BaseURL 设置url前缀

func (Request) Clone

func (r Request) Clone() Request

func (Request) Cookie

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

Cookie 设置请求 Cookie

func (Request) Del

func (r Request) Del(url string, body ...interface{}) (*Response, error)

Del 发起 delete 请求,body 是请求带的参数,可使用json字符串或者结构体

func (Request) Do

func (r Request) Do(method, url string, args ...interface{}) (*Response, error)

Do 发出请求,method 请求方法,url 请求地址, query 查询参数,body 请求数据,file 文件对象/地址

func (Request) Get

func (r Request) Get(url string, args ...interface{}) (*Response, error)

Get 发起 get 请求, query 查询参数

func (Request) Head

func (r Request) Head(url string, args ...interface{}) (*Response, error)

Head 发起 head 请求

func (Request) Header

func (r Request) Header(key, val string) Request

Header 设置请求 Header

func (Request) Options

func (r Request) Options(url string, args ...interface{}) (*Response, error)

Options 发起 options 请求,query 查询参数

func (Request) Patch

func (r Request) Patch(url string, body ...interface{}) (*Response, error)

Patch 发起 patch 请求,body 是请求带的参数,可使用json字符串或者结构体

func (Request) Post

func (r Request) Post(url string, body ...interface{}) (*Response, error)

Post 发起 post 请求,body 是请求带的参数,可使用json字符串或者结构体

func (Request) PostFile

func (r Request) PostFile(url string, file interface{}, body interface{}) (*Response, error)

PostFile 发起 post 请求上传文件,将使用表单提交,file 是文件地址或者文件流, body 是请求带的参数,可使用json字符串或者结构体

func (Request) Proxy

func (r Request) Proxy(url string) Request

Proxy 设置请求代理

func (Request) Put

func (r Request) Put(url string, body ...interface{}) (*Response, error)

Put 发起 put 请求,body 是请求带的参数,可使用json字符串或者结构体

func (Request) PutFile

func (r Request) PutFile(url string, file interface{}, body interface{}) (*Response, error)

PutFile 发起 put 请求上传文件,将使用表单提交,file 是文件地址或者文件流, body 是请求带的参数,可使用json字符串或者结构体

func (Request) Query

func (r Request) Query(query interface{}) Request

Query 增加查询参数

func (Request) Timeout

func (r Request) Timeout(timeout time.Duration) Request

Timeout 请求超时时间

func (Request) Type

func (r Request) Type(name string) Request

Type 请求提交方式,默认json

func (Request) UseRequest

func (r Request) UseRequest(mdl requestMiddlewareHandler) Request

UseRequest 增加请求中间件

func (Request) UseResponse

func (r Request) UseResponse(mdl responseMidlewareHandler) Request

UseResponse 增加响应中间件

func (Request) UserAgent

func (r Request) UserAgent(name string) Request

UserAgent 设置请求 user-agent,默认是 chrome 75.0

type Response

type Response struct {
	Request *Request
	Raw     *http.Response
	Body    []byte
	Errs    ResponseError
}

Response 回应对象

func Del

func Del(url string, body interface{}) (*Response, error)

Del 发起 delete 请求,body 是请求带的参数,可使用json字符串或者结构体

func Get

func Get(url string, query interface{}) (*Response, error)

Get 发起 get 请求, query 查询参数

func Head(url string, query interface{}) (*Response, error)

Head 发起 head 请求

func NewResponse

func NewResponse() *Response

NewResponse 新建回应对象

func Options

func Options(url string, query interface{}) (*Response, error)

Options 发起 options 请求,query 查询参数

func Patch

func Patch(url string, body interface{}) (*Response, error)

Patch 发起 patch 请求,body 是请求带的参数,可使用json字符串或者结构体

func Post

func Post(url string, body interface{}) (*Response, error)

Post 发起 post 请求,body 是请求带的参数,可使用json字符串或者结构体

func PostFile

func PostFile(url string, file interface{}, body interface{}) (*Response, error)

PostFile 发起 post 请求上传文件,将使用表单提交,file 是文件地址或者文件流, body 是请求带的参数,可使用json字符串或者结构体

func Put

func Put(url string, body interface{}) (*Response, error)

Put 发起 put 请求,body 是请求带的参数,可使用json字符串或者结构体

func PutFile

func PutFile(url string, file interface{}, body interface{}) (*Response, error)

PutFile 发起 put 请求上传文件,将使用表单提交,file 是文件地址或者文件流, body 是请求带的参数,可使用json字符串或者结构体

func (*Response) Byte

func (r *Response) Byte() []byte

Byte 获取响应字节

func (*Response) Cookies

func (r *Response) Cookies() []*http.Cookie

Cookies 获取响应 cookie

func (*Response) Err

func (r *Response) Err() error

Err 获取响应错误

func (*Response) Header

func (r *Response) Header() http.Header

Header 获取响应header

func (*Response) IsError

func (r *Response) IsError() bool

IsError 是否响应错误

func (*Response) JSON

func (r *Response) JSON(v interface{}) error

JSON 根据json绑定结构体

func (*Response) Status

func (r *Response) Status() int

Status 获取响应状态码

func (*Response) String

func (r *Response) String() string

String 获取响应字符串

type ResponseError

type ResponseError []error

ResponseError 响应错误对象

func (ResponseError) Add

func (e ResponseError) Add(err error) ResponseError

Add 增加错误

func (ResponseError) Error

func (e ResponseError) Error() string

Error 实现 error 接口

func (ResponseError) HasErr

func (e ResponseError) HasErr() bool

HasErr 是否有错误

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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