httplib

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package httplib is used as http.Client Usage:

import "github.com/bhojpur/web/pkg/httplib"

b := httplib.Post("http://app.bhojpur.net/")
b.Param("username","bhojpur")
b.Param("password","123456")
b.PostFile("uploadfile1", "httplib.pdf")
b.PostFile("uploadfile2", "httplib.txt")
str, err := b.String()
if err != nil {
	t.Fatal(err)
}
fmt.Println(str)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDefaultSetting

func SetDefaultSetting(setting BhojpurHTTPSettings)

SetDefaultSetting overwrites default settings

func TimeoutDialer

func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error)

TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.

Types

type BhojpurHTTPRequest

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

BhojpurHTTPRequest provides more useful methods than http.Request for requesting a url.

func Delete

func Delete(url string) *BhojpurHTTPRequest

Delete returns *BhojpurHttpRequest DELETE method.

func Get

func Get(url string) *BhojpurHTTPRequest

Get returns *BhojpurHttpRequest with GET method.

func Head(url string) *BhojpurHTTPRequest

Head returns *BhojpurHttpRequest with HEAD method.

func NewBhojpurRequest

func NewBhojpurRequest(rawurl, method string) *BhojpurHTTPRequest

NewBhojpurRequest returns *BhojpurHttpRequest with specific method

func Post

func Post(url string) *BhojpurHTTPRequest

Post returns *BhojpurHttpRequest with POST method.

func Put

func Put(url string) *BhojpurHTTPRequest

Put returns *BhojpurHttpRequest with PUT method.

func (*BhojpurHTTPRequest) AddFilters

func (b *BhojpurHTTPRequest) AddFilters(fcs ...FilterChain) *BhojpurHTTPRequest

AddFilters adds filter

func (*BhojpurHTTPRequest) Body

func (b *BhojpurHTTPRequest) Body(data interface{}) *BhojpurHTTPRequest

Body adds request raw body. Supports string and []byte.

func (*BhojpurHTTPRequest) Bytes

func (b *BhojpurHTTPRequest) Bytes() ([]byte, error)

Bytes returns the body []byte in response. Calls Response inner.

func (*BhojpurHTTPRequest) Debug

func (b *BhojpurHTTPRequest) Debug(isdebug bool) *BhojpurHTTPRequest

Debug sets show debug or not when executing request.

func (*BhojpurHTTPRequest) DoRequest

func (b *BhojpurHTTPRequest) DoRequest() (resp *http.Response, err error)

DoRequest executes client.Do

func (*BhojpurHTTPRequest) DoRequestWithCtx

func (b *BhojpurHTTPRequest) DoRequestWithCtx(ctx context.Context) (resp *http.Response, err error)

func (*BhojpurHTTPRequest) DumpBody

func (b *BhojpurHTTPRequest) DumpBody(isdump bool) *BhojpurHTTPRequest

DumpBody sets the DumbBody field

func (*BhojpurHTTPRequest) DumpRequest

func (b *BhojpurHTTPRequest) DumpRequest() []byte

DumpRequest returns the DumpRequest

func (*BhojpurHTTPRequest) GetRequest

func (b *BhojpurHTTPRequest) GetRequest() *http.Request

GetRequest returns the request object

func (*BhojpurHTTPRequest) Header

func (b *BhojpurHTTPRequest) Header(key, value string) *BhojpurHTTPRequest

Header adds header item string in request.

func (*BhojpurHTTPRequest) JSONBody

func (b *BhojpurHTTPRequest) JSONBody(obj interface{}) (*BhojpurHTTPRequest, error)

JSONBody adds the request raw body encoded in JSON.

func (*BhojpurHTTPRequest) Param

func (b *BhojpurHTTPRequest) Param(key, value string) *BhojpurHTTPRequest

Param adds query param in to request. params build query string as ?key1=value1&key2=value2...

func (*BhojpurHTTPRequest) PostFile

func (b *BhojpurHTTPRequest) PostFile(formname, filename string) *BhojpurHTTPRequest

PostFile adds a post file to the request

func (*BhojpurHTTPRequest) Response

func (b *BhojpurHTTPRequest) Response() (*http.Response, error)

Response executes request client gets response manually.

func (*BhojpurHTTPRequest) Retries

func (b *BhojpurHTTPRequest) Retries(times int) *BhojpurHTTPRequest

Retries sets Retries times. default is 0 (never retry) -1 retry indefinitely (forever) Other numbers specify the exact retry amount

func (*BhojpurHTTPRequest) RetryDelay

func (b *BhojpurHTTPRequest) RetryDelay(delay time.Duration) *BhojpurHTTPRequest

RetryDelay sets the time to sleep between reconnection attempts

func (*BhojpurHTTPRequest) SetBasicAuth

func (b *BhojpurHTTPRequest) SetBasicAuth(username, password string) *BhojpurHTTPRequest

SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.

func (*BhojpurHTTPRequest) SetCheckRedirect

func (b *BhojpurHTTPRequest) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *BhojpurHTTPRequest

SetCheckRedirect specifies the policy for handling redirects.

If CheckRedirect is nil, the Client uses its default policy, which is to stop after 10 consecutive requests.

func (*BhojpurHTTPRequest) SetCookie

func (b *BhojpurHTTPRequest) SetCookie(cookie *http.Cookie) *BhojpurHTTPRequest

SetCookie adds a cookie to the request.

func (*BhojpurHTTPRequest) SetEnableCookie

func (b *BhojpurHTTPRequest) SetEnableCookie(enable bool) *BhojpurHTTPRequest

SetEnableCookie sets enable/disable cookiejar

func (*BhojpurHTTPRequest) SetFilters

func (b *BhojpurHTTPRequest) SetFilters(fcs ...FilterChain) *BhojpurHTTPRequest

SetFilters will use the filter as the invocation filters

func (*BhojpurHTTPRequest) SetHost

func (b *BhojpurHTTPRequest) SetHost(host string) *BhojpurHTTPRequest

SetHost set the request host

func (*BhojpurHTTPRequest) SetProtocolVersion

func (b *BhojpurHTTPRequest) SetProtocolVersion(vers string) *BhojpurHTTPRequest

SetProtocolVersion sets the protocol version for incoming requests. Client requests always use HTTP/1.1.

func (*BhojpurHTTPRequest) SetProxy

func (b *BhojpurHTTPRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *BhojpurHTTPRequest

SetProxy sets the HTTP proxy example:

func(req *http.Request) (*url.URL, error) {
	u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
	return u, nil
}

func (*BhojpurHTTPRequest) SetTLSClientConfig

func (b *BhojpurHTTPRequest) SetTLSClientConfig(config *tls.Config) *BhojpurHTTPRequest

SetTLSClientConfig sets TLS connection configuration if visiting HTTPS url.

func (*BhojpurHTTPRequest) SetTimeout

func (b *BhojpurHTTPRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BhojpurHTTPRequest

SetTimeout sets connect time out and read-write time out for BhojpurRequest.

func (*BhojpurHTTPRequest) SetTransport

func (b *BhojpurHTTPRequest) SetTransport(transport http.RoundTripper) *BhojpurHTTPRequest

SetTransport sets the transport field

func (*BhojpurHTTPRequest) SetUserAgent

func (b *BhojpurHTTPRequest) SetUserAgent(useragent string) *BhojpurHTTPRequest

SetUserAgent sets User-Agent header field

func (*BhojpurHTTPRequest) Setting

Setting changes request settings

func (*BhojpurHTTPRequest) String

func (b *BhojpurHTTPRequest) String() (string, error)

String returns the body string in response. Calls Response inner.

func (*BhojpurHTTPRequest) ToFile

func (b *BhojpurHTTPRequest) ToFile(filename string) error

ToFile saves the body data in response to one file. Calls Response inner.

func (*BhojpurHTTPRequest) ToJSON

func (b *BhojpurHTTPRequest) ToJSON(v interface{}) error

ToJSON returns the map that marshals from the body bytes as json in response. Calls Response inner.

func (*BhojpurHTTPRequest) ToXML

func (b *BhojpurHTTPRequest) ToXML(v interface{}) error

ToXML returns the map that marshals from the body bytes as xml in response . Calls Response inner.

func (*BhojpurHTTPRequest) ToYAML

func (b *BhojpurHTTPRequest) ToYAML(v interface{}) error

ToYAML returns the map that marshals from the body bytes as yaml in response . Calls Response inner.

func (*BhojpurHTTPRequest) XMLBody

func (b *BhojpurHTTPRequest) XMLBody(obj interface{}) (*BhojpurHTTPRequest, error)

XMLBody adds the request raw body encoded in XML.

func (*BhojpurHTTPRequest) YAMLBody

func (b *BhojpurHTTPRequest) YAMLBody(obj interface{}) (*BhojpurHTTPRequest, error)

YAMLBody adds the request raw body encoded in YAML.

type BhojpurHTTPSettings

type BhojpurHTTPSettings struct {
	ShowDebug        bool
	UserAgent        string
	ConnectTimeout   time.Duration
	ReadWriteTimeout time.Duration
	TLSClientConfig  *tls.Config
	Proxy            func(*http.Request) (*url.URL, error)
	Transport        http.RoundTripper
	CheckRedirect    func(req *http.Request, via []*http.Request) error
	EnableCookie     bool
	Gzip             bool
	DumpBody         bool
	Retries          int // if set to -1 means will retry forever
	RetryDelay       time.Duration
	FilterChains     []FilterChain
}

BhojpurHTTPSettings is the http.Client setting

type Filter

type Filter func(ctx context.Context, req *BhojpurHTTPRequest) (*http.Response, error)

type FilterChain

type FilterChain func(next Filter) Filter

Directories

Path Synopsis
filter

Jump to

Keyboard shortcuts

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