fasthttp

package module
v0.0.0-...-53ed216 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2021 License: MIT Imports: 15 Imported by: 1

README

[[TOC]]

fasthttp-client

基于fasthttp的http请求客户端,用于快速构建http请求

更新

  • 2020-12-15
    • 修复get时url自带query导致最后加了一个?的bug
  • 2020-12-14
    • 返回添加header
    • 重新设计了请求时入参的结构(param,header,cookie等)

功能

  • get
  • post
  • sendFile
  • 支持使用tls
  • 支持proxy(全局)

快速开始

  • get

    res, err = NewClient().
    		AddParam("param1", "param1").
    		AddParams(
    			NewParams().
    				Set("param2", "param2").
    				Set("param3", "param3")).
    		AddHeader("header1", "header1").
    		AddHeaders(
    			NewHeaders().
    				Set("header2", "header2").
    				Set("header3", "header3")).
    		AddCookie("cookie1", "cookie1").
    		AddCookies(
    			NewCookies().
    				Set("cookie1", "cookie1").
    				Set("cookie2", "cookie2")).
    		Get("http://httpbin.org/get")
    
  • post

    res, err = NewClient().
    		AddBodyStruct(
    			struct {
    				Request string `json:"request" form:"request"`
    				Num     int    `json:"num"`
    			}{
    				Request: "test",
    				Num:     1,
    			},
    		).
    		AddHeader("header1", "header1").
    		AddHeaders(
    			NewHeaders().
    				Set("header2", "header2").
    				Set("header3", "header3"),
    			//"content-type": "application/json",
    		).
    		AddCookie("cookie1", "cookie1").
    		AddCookies(NewCookies().
    			Set("cookie1", "cookie1").
    			Set("cookie2", "cookie2")).
    		Post("http://httpbin.org/post")
    
  • sendFile

    res, err = NewClient().
    		AddFile("a", "/Users/chuwt/Downloads/test.jpg").
    		AddFiles(
    			NewFiles().
    				Set("b", "/Users/chuwt/Downloads/test.jpg"),
    		).
    		SendFile("http://httpbin.org/post")
    
  • tls

    client = NewClient()
    client.SetCrt(certPath, certKey).Get("")
    
  • ps

    • client.SetTimeout 非线程安全,倾向于做为全局(实例)配置使用
    • client.SetCrt 非线程安全,倾向于做为全局(实例)配置使用
  • 说明

    • 暂时不支持get中带有body的请求
    • post 的content-type默认为application/x-www-form-urlencoded
    • 根据fasthttp的issue, client不支持获取返回的类似io.Reader,需要等待所有 返回都被接收后才返回client.Do, 所以没法支持 chunked 返回
    • 不过这个人写了一个demo用来支持获取io.Reader的body,但是没有merge到主分支上去
    • 官方回复

todo

  • 单个请求的proxy 支持

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	EmptyUrlErr  = errors.New("empty url")
	EmptyFileErr = errors.New("empty file")
)

Functions

func NewClientPool

func NewClientPool() sync.Pool

Types

type Client

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

func NewClient

func NewClient() *Client

func (*Client) AddBodyByte

func (c *Client) AddBodyByte(body []byte) *Client

func (*Client) AddBodyBytes

func (c *Client) AddBodyBytes(bodyBytes []byte) *Client

func (*Client) AddBodyStruct

func (c *Client) AddBodyStruct(object interface{}) *Client

func (*Client) AddCookie

func (c *Client) AddCookie(key, value string) *Client

func (*Client) AddCookies

func (c *Client) AddCookies(cookies Mapper) *Client

func (*Client) AddFile

func (c *Client) AddFile(fileName, filePath string) *Client

func (*Client) AddFiles

func (c *Client) AddFiles(files Mapper) *Client

func (*Client) AddHeader

func (c *Client) AddHeader(key, value string) *Client

func (*Client) AddHeaders

func (c *Client) AddHeaders(headers Mapper) *Client

func (*Client) AddParam

func (c *Client) AddParam(key, value string) *Client

func (*Client) AddParams

func (c *Client) AddParams(params Mapper) *Client

func (*Client) Get

func (c *Client) Get(rawUrl string) (*Response, error)

func (*Client) Post

func (c *Client) Post(url string) (*Response, error)

func (*Client) SendFile

func (c *Client) SendFile(url string, options ...RequestOption) (*Response, error)

func (*Client) SetCrt

func (c *Client) SetCrt(certPath, keyPath string) *Client

func (*Client) SetProxy

func (c *Client) SetProxy(proxy string) *Client

func (*Client) SetTimeout

func (c *Client) SetTimeout(duration time.Duration) *Client

type Mapper

type Mapper map[string]string

func NewCookies

func NewCookies() Mapper

func NewFiles

func NewFiles() Mapper

func NewHeaders

func NewHeaders() Mapper

func NewParams

func NewParams() Mapper

func (Mapper) Get

func (m Mapper) Get(key string) string

func (Mapper) Set

func (m Mapper) Set(key, value string) Mapper

type RequestCookies

type RequestCookies struct{ Mapper }

type RequestFiles

type RequestFiles struct{ Mapper }

type RequestHeaders

type RequestHeaders struct{ Mapper }

type RequestOption

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

type RequestParams

type RequestParams struct{ Mapper }

type Response

type Response struct {
	StatusCode int
	Body       []byte
	Header     RequestHeaders
	Cookie     RequestCookies
}

Jump to

Keyboard shortcuts

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