guzzle

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2022 License: MIT Imports: 9 Imported by: 0

README

封装golang http请求

设置host发送请求

...

client := NewClient(WithHost("http://localhost"))

response, err := client.Get("/api/test")
if err != nil {
    return err
}

fmt.Println(response.String())

data := map[string]interface{}
err = response.JSON(data)
if err != nil {
    return err
}

...

不设置host发送请求

...
client := NewClient()

response, err := client.Get("http://localhost/api/test")
if err != nil {
    return err
}

fmt.Println(response.String())

data := map[string]interface{}
err = response.JSON(data)
if err != nil {
    return err
}

...

设置query请求

...

client := NewClient()

response, err := client.RequestQuery(map[string][]string{
    "id": {"1", "2", "3"},
    "name": {"zhangsan"},
    }).Get("/api/test")
if err != nil {
    return err
}

fmt.Println(response.String())

data := map[string]interface{}
err = response.JSON(data)
if err != nil {
    return err
}

...

设置Json请求体

...

client := NewClient()

response, err := client.RequestJSON(map[string]interface{}{
        "id":   "1",
        "name": "zhangsan",
    }).Post("/api/test")    
if err != nil {
    return err
}

fmt.Println(response.String())

data := map[string]interface{}
err = response.JSON(data)
if err != nil {
    return err
}

...

Documentation

Overview

Package guzzle @Author: asus @Description: $ @File: client @Data: 2022/1/2016:41

Package guzzle @Author: asus @Description: $ @File: options @Data: 2022/1/2016:41

Package guzzle @Author: asus @Description: $ @File: param @Data: 2022/1/2016:51

Package guzzle @Author: asus @Description: $ @File: response @Data: 2022/1/2017:59

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Host  string
	Error error
	// contains filtered or unexported fields
}

func NewClient

func NewClient(options ...Options) *Client

func (*Client) Delete

func (c *Client) Delete(path string) (*Response, error)

func (*Client) Get

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

func (*Client) Post

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

func (*Client) Put

func (c *Client) Put(path string) (*Response, error)

func (*Client) Request

func (c *Client) Request(method, path string) (*Response, error)

Request 发送请求

func (*Client) RequestForm

func (c *Client) RequestForm(form map[string][]string) *Client

RequestForm 设置Form请求体

func (*Client) RequestHeader

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

RequestHeader 设置请求头

func (*Client) RequestHeaders

func (c *Client) RequestHeaders() map[string]string

RequestHeaders 获取请求Header头信息

func (*Client) RequestJSON

func (c *Client) RequestJSON(v interface{}) *Client

RequestJSON 设置Json请求体

func (*Client) RequestQuery

func (c *Client) RequestQuery(query map[string][]string) *Client

RequestQuery 设置请求参数

func (*Client) RequestSetBody

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

RequestSetBody 设置请求体

type Options

type Options interface {
	// contains filtered or unexported methods
}

type OptionsFunc

type OptionsFunc func(*Client)

func WithHead

func WithHead(key, value string) OptionsFunc

func WithHost

func WithHost(host string) OptionsFunc

设置请求头

func WithTimeOut

func WithTimeOut(timeout int64) OptionsFunc

设置超时时间

type Request

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

func NewRequest

func NewRequest() *Request

func (*Request) Body

func (r *Request) Body(body []byte)

func (*Request) Form

func (r *Request) Form(form map[string][]string)

func (*Request) Header

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

func (*Request) Headers

func (r *Request) Headers() map[string]string

func (*Request) JSON

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

func (*Request) Query

func (r *Request) Query(query map[string][]string)

Query 设置请求参数

func (*Request) String

func (r *Request) String() string

String 返回请求的string内容

type Response

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

func NewResponse

func NewResponse(response *http.Response) (*Response, error)

func (*Response) Body

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

func (*Response) Cookie

func (r *Response) Cookie(key string) string

func (*Response) Cookies

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

func (*Response) Header

func (r *Response) Header(key string) string

func (*Response) Headers

func (r *Response) Headers() map[string]string

func (*Response) JSON

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

func (*Response) Status

func (r *Response) Status() int

func (*Response) String

func (r *Response) String() string

Jump to

Keyboard shortcuts

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