httpchain

package module
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

go-http-chain

GitHub Repo stars GitHub issues GitHub License GitHub release (with filter) Go Report Card All Contributors

A chainable Go HTTP library for streamlined request and response management.


English | 中文简体

Install

go get github.com/chenmingyong0423/go-http-chain

Usage

// Create a new default client and set global Header parameters for the client
client := httpchain.NewDefault().SetHeader("X-Global-Param", "go-http-chain")
// Create a Request specifying a GET request; the client's Headers and Query parameters will be passed to this Request
// Retrieve the *http.Response
resp, err := client.Get("localhost:8080/test").
		SetHeader("name", "Mingyong Chen").
		SetQuery("name", "Mingyong Chen").
		Call(context.Background()).Result()

// Directly parse the response result into a specified struct
var result map[string]any
err = client.Get("localhost:8080/test").
		SetHeader("name", "Mingyong Chen").
		SetQuery("name", "Mingyong Chen").
		Call(context.Background()).DecodeRespBody(&result)

Documentation

Index

Constants

View Source
const (
	HeaderContentType = "Content-Type"

	ContentTypeTextPlain                    = "text/plain"
	ContentTypeTextPlainCharacterUTF8       = "text/plain; charset=utf-8"
	ContentTypeApplicationJSON              = "application/json"
	ContentTypeApplicationJSONCharacterUTF8 = "application/json; charset=utf-8"
	ContentTypeApplicationXML               = "application/xml"
	ContentTypeApplicationXMLCharacterUTF8  = "application/xml; charset=utf-8"
	ContentTypeTextXML                      = "text/xml"
	ContentTypeTextXMLCharacterUTF8         = "text/xml; charset=utf-8"
	ContentTypeApplicationOctetStream       = "application/octet-stream"
	ContentTypeMultipartFormData            = "multipart/form-data"
	ContentTypeFormURLEncoded               = "application/x-www-form-urlencoded"
)

Variables

This section is empty.

Functions

func DecodeRespBody

func DecodeRespBody(resp *http.Response, dst any) error

func IsUnsupportedContentTypeError added in v0.3.0

func IsUnsupportedContentTypeError(err error) bool

Types

type BodyEncodeFunc

type BodyEncodeFunc func(body any) (io.Reader, error)

type Client

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

func NewDefault

func NewDefault() *Client

func NewWithClient

func NewWithClient(client *http.Client) *Client

func (*Client) AddHeader

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

func (*Client) AddQuery

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

func (*Client) Connect

func (c *Client) Connect(url string) *Request

func (*Client) Delete

func (c *Client) Delete(url string) *Request

func (*Client) Get

func (c *Client) Get(url string) *Request

func (*Client) Head

func (c *Client) Head(url string) *Request

func (*Client) Options

func (c *Client) Options(url string) *Request

func (*Client) Patch

func (c *Client) Patch(url string) *Request

func (*Client) Post

func (c *Client) Post(url string) *Request

func (*Client) Put

func (c *Client) Put(url string) *Request

func (*Client) Request

func (c *Client) Request(url, method string) *Request

func (*Client) SetHeader

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

func (*Client) SetQuery

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

func (*Client) Trace

func (c *Client) Trace(url string) *Request

type Request

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

func (*Request) AddHeader

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

func (*Request) AddQuery

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

func (*Request) Call

func (r *Request) Call(ctx context.Context) *Response

func (*Request) SetBody

func (r *Request) SetBody(body any) *Request

func (*Request) SetBodyEncodeFunc

func (r *Request) SetBodyEncodeFunc(fn BodyEncodeFunc) *Request

func (*Request) SetHeader

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

func (*Request) SetQuery

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

type Response

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

func (*Response) DecodeRespBody

func (r *Response) DecodeRespBody(dst any) error

func (*Response) Result

func (r *Response) Result() (*http.Response, error)

type UnsupportedContentTypeError added in v0.3.0

type UnsupportedContentTypeError struct {
	ContentType string
}

func (*UnsupportedContentTypeError) Error added in v0.3.0

Jump to

Keyboard shortcuts

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