gohttp

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 9 Imported by: 0

README

gohttp

http请求客户端,只支持返回json格式。


使用示例

标准CURD

Get、Delete请求示例

	req := gohttp.Request{}
	req.SetURL("http://localhost:8080/user")
	req.SetHeaders(map[string]string{
		"Authorization": "Bearer token",
	})
	req.SetParams(gohttp.KV{
		"id": 123,
	})

	resp, err := req.GET()
	// resp, err := req.Delete()

	result := &gohttp.StdResult{} // 可以定义其他结构体接收数据
	err = resp.BindJSON(result)

Post、Put、Patch请求示例

	req := gohttp.Request{}
	req.SetURL("http://localhost:8080/user")
	req.SetHeaders(map[string]string{
		"Authorization": "Bearer token",
	})

	// body为结构体
    type User struct{
        Name string
        Email string
    }
    body := &User{"foo", "foo@bar.com"}
    req.SetJSONBody(body)
    // 或者 body为json
    // req.SetBody(`{"name":"foo", "email":"foo@bar.com"}`)

	resp, err := req.Post()
	// resp, err := req.Put()
	// resp, err := req.Patch()

	result := &gohttp.StdResult{} // 可以定义其他结构体接收数据
	err = resp.BindJSON(result)

简化版CRUD

不支持设置header、超时等

    url := "http://localhost:8080/user"
    params := gohttp.KV{"id":123}
    result := &gohttp.StdResult{} // 可以定义其他结构体接收数据

    // Get
    err := gohttp.Get(result, url)
    err := gohttp.Get(result, url, params)

    // Delete
    err := gohttp.Delete(result, url)
    err := gohttp.Delete(result, url, params)

    type User struct{
        Name string
        Email string
    }
    body := &User{"foo", "foo@bar.com"}

    // Post
    err := gohttp.Post(result, url, body)
    // Put
    err := gohttp.Put(result, url, body)
    // Patch
    err := gohttp.Patch(result, url, body)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(result interface{}, url string, params ...KV) error

Delete 请求,返回自定义json格式

func Get

func Get(result interface{}, url string, params ...KV) error

Get 请求,返回自定义json格式

func Patch

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

Patch 请求,返回自定义json格式

func Post

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

Post 请求,返回自定义json格式

func Put

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

Put 请求,返回自定义json格式

Types

type KV

type KV map[string]interface{}

KV string:interface{}

type Request

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

Request HTTP request

func (*Request) CustomRequest

func (req *Request) CustomRequest(f func(req *http.Request, data *bytes.Buffer)) *Request

CustomRequest 自定义Request, 如添加sign, 设置header等

func (*Request) DELETE

func (req *Request) DELETE() (*Response, error)

DELETE 发送DELETE请求

func (*Request) Do

func (req *Request) Do(method string, data interface{}) (*Response, error)

Do a request

func (*Request) GET

func (req *Request) GET() (*Response, error)

GET 发送GET请求

func (*Request) PATCH

func (req *Request) PATCH() (*Response, error)

PATCH 发送PATCH请求

func (*Request) POST

func (req *Request) POST() (*Response, error)

POST 发送POST请求

func (*Request) PUT

func (req *Request) PUT() (*Response, error)

PUT 发送PUT请求

func (*Request) Reset

func (req *Request) Reset()

Reset set all fields to default value, use at pool

func (*Request) Response

func (req *Request) Response() (*Response, error)

Response return response

func (*Request) SetBody

func (req *Request) SetBody(body string) *Request

SetBody 设置Body数据

func (*Request) SetContentType

func (req *Request) SetContentType(a string) *Request

SetContentType 设置ContentType

func (*Request) SetHeader

func (req *Request) SetHeader(k, v string) *Request

SetHeader 设置Request Header 的值

func (*Request) SetHeaders added in v1.2.0

func (req *Request) SetHeaders(headers map[string]string) *Request

SetHeaders 设置Request Headers 的值

func (*Request) SetJSONBody

func (req *Request) SetJSONBody(body interface{}) *Request

SetJSONBody 设置Body数据, JSON格式

func (*Request) SetParam

func (req *Request) SetParam(k string, v interface{}) *Request

SetParam 设置URL后的参数

func (*Request) SetParams

func (req *Request) SetParams(params map[string]interface{}) *Request

SetParams 设置URL后的参数

func (*Request) SetTimeout

func (req *Request) SetTimeout(t time.Duration) *Request

SetTimeout 超时时间

func (*Request) SetURL

func (req *Request) SetURL(path string) *Request

SetURL 设置URL

type Response

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

Response HTTP response

func (*Response) BindJSON

func (resp *Response) BindJSON(v interface{}) error

BindJSON parses the response's body as JSON

func (*Response) BodyString

func (resp *Response) BodyString() (string, error)

BodyString 返回HttpResponse的body数据

func (*Response) Error

func (resp *Response) Error() error

Error return err

func (*Response) ReadBody

func (resp *Response) ReadBody() ([]byte, error)

ReadBody 返回HttpResponse的body数据

type StdResult added in v1.2.0

type StdResult struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data,omitempty"`
}

StdResult 标准返回数据

Jump to

Keyboard shortcuts

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