httpcli

package
v1.0.38 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2024 License: MIT Imports: 9 Imported by: 0

README

httpcli

httcli is a simple HTTP request client, which only supports returning json format.


Example of use

Request way 1

    import "github.com/18721889353/sunshine/pkg/httpcli"

    type User struct{
        Name string
        Email string
    }

    url := "http://localhost:8080/user"
    params := httpcli.KV{"id":123}
    headers := map[string]string{"Authorization": "Bearer token"}
    body := &User{"foo", "foo@bar.com"}
    result := &httpcli.StdResult{} // other structures can be defined to receive data

    var err error

    // Get
    err = httpcli.Get(result, url)
    err = httpcli.Get(result, url, httpcli.WithParams(params))
    err = httpcli.Get(result, url, httpcli.WithParams(params), httpcli.WithHeaders(headers))

    // Delete
    err = httpcli.Delete(result, url)
    err = httpcli.Delete(result, httpcli.WithParams(params))
    err = httpcli.Delete(result, httpcli.WithParams(params), httpcli.WithHeaders(headers))

    // Post
    err = httpcli.Post(result, url, body)
    err = httpcli.Post(result, url, body, httpcli.WithParams(params))
    err = httpcli.Delete(result, httpcli.WithParams(params), httpcli.WithHeaders(headers))
    // Put
    err := httpcli.Put(result, url, body)
    // Patch
    err := httpcli.Patch(result, url, body)

Request way 2

Get, Delete request example.

    import "github.com/18721889353/sunshine/pkg/httpcli"

    url := "http://localhost:8080/user"
    headers := map[string]string{"Authorization": "Bearer token"}
    params := httpcli.KV{"id": 123}

    cli := httpcli.New().SetURL(url).SetHeaders(headers).SetParams(params)

    // Get
    resp, err := cli.GET()
    // Delete
    // resp, err := cli.Delete()

    defer resp.Body.Close()

    result := &httpcli.StdResult{} // other structures can be defined to receive data
    err = resp.BindJSON(result)

Post, Put, Patch request example.

    import "github.com/18721889353/sunshine/pkg/httpcli"


    type User struct{
        Name string
        Email string
    }

    body := &User{"foo", "foo@bar.com"}
    url := "http://localhost:8080/user"
    headers := map[string]string{"Authorization": "Bearer token"}

    cli := httpcli.New().SetURL(url).SetHeaders(headers).SetBody(body)

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

   defer resp.Body.Close()

    result := &httpcli.StdResult{} // other structures can be defined to receive data
    err = resp.BindJSON(result)

Documentation

Overview

Package httpcli is http request client, which only supports return json format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(result interface{}, urlStr string, opts ...Option) error

Delete request, return custom json format

func Get

func Get(result interface{}, urlStr string, opts ...Option) error

Get request, return custom json format

func Patch

func Patch(result interface{}, urlStr string, body interface{}, opts ...Option) error

Patch request, return custom json format

func Post

func Post(result interface{}, urlStr string, body interface{}, opts ...Option) error

Post request, return custom json format

func Put

func Put(result interface{}, urlStr string, body interface{}, opts ...Option) error

Put request, return custom json format

Types

type KV

type KV = map[string]interface{}

KV string:interface{}

type Option

type Option func(*options)

Option set options.

func WithHeaders

func WithHeaders(headers map[string]string) Option

WithHeaders set headers

func WithParams

func WithParams(params map[string]interface{}) Option

WithParams set params

func WithTimeout

func WithTimeout(t time.Duration) Option

WithTimeout set timeout

type Request

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

Request HTTP request

func New

func New() *Request

New create a new Request

func (*Request) CustomRequest

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

CustomRequest customize request, e.g. add sign, set header, etc.

func (*Request) DELETE

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

DELETE send a DELETE request

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 send a GET request

func (*Request) PATCH

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

PATCH send PATCH requests

func (*Request) POST

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

POST send a POST request

func (*Request) PUT

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

PUT send a PUT request

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 interface{}) *Request

SetBody set body data, support string and []byte, if it is not string, it will be json marshal.

func (*Request) SetContentType

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

SetContentType set ContentType

func (*Request) SetHeader

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

SetHeader set the value of the request header

func (*Request) SetHeaders

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

SetHeaders set the value of Request Headers

func (*Request) SetParam

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

SetParam parameters after setting the URL

func (*Request) SetParams

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

SetParams parameters after setting the URL

func (*Request) SetTimeout

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

SetTimeout set timeout

func (*Request) SetURL

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

SetURL set 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 returns the body data of the HttpResponse

func (*Response) Error

func (resp *Response) Error() error

Error return err

func (*Response) ReadBody

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

ReadBody returns the body data of the HttpResponse

type StdResult

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

StdResult standard return data

Jump to

Keyboard shortcuts

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