Fetch
The Fetch type is the main client for making HTTP requests.
Functions
New
Creates a new fetch client.
func New(cfg ...*Config) *FetchParameters:
cfg: Optional configuration
Example:
f := fetch.New()
f := fetch.New(&fetch.Config{
Timeout: 10 * time.Second,
})Create
Creates a new fetch with base URL.
func Create(baseURL string) *FetchParameters:
baseURL: Base URL for all requests
Example:
f := fetch.Create("https://api.example.com")Methods
SetURL
Sets the request URL.
func (f *Fetch) SetURL(url string) *FetchSetBaseURL
Sets the base URL.
func (f *Fetch) SetBaseURL(url string) *FetchSetMethod
Sets the HTTP method.
func (f *Fetch) SetMethod(method string) *FetchSetHeader
Sets a request header.
func (f *Fetch) SetHeader(key, value string) *FetchSetQuery
Sets a query parameter.
func (f *Fetch) SetQuery(key, value string) *FetchSetBody
Sets the request body.
func (f *Fetch) SetBody(body Body) *FetchSetTimeout
Sets the request timeout.
func (f *Fetch) SetTimeout(timeout time.Duration) *FetchSetBasicAuth
Sets basic authentication.
func (f *Fetch) SetBasicAuth(username, password string) *FetchSetBearerToken
Sets bearer token authentication.
func (f *Fetch) SetBearerToken(token string) *FetchSetProxy
Sets proxy server.
func (f *Fetch) SetProxy(proxy string) *FetchSetContext
Sets the context for cancellation.
func (f *Fetch) SetContext(ctx context.Context) *FetchSetProgressCallback
Sets progress callback for upload/download.
func (f *Fetch) SetProgressCallback(callback func(percent int64, current, total int64)) *FetchGet
Sets HTTP method to GET.
func (f *Fetch) Get(url string, config ...*Config) *FetchPost
Sets HTTP method to POST.
func (f *Fetch) Post(url string, config ...*Config) *FetchPut
Sets HTTP method to PUT.
func (f *Fetch) Put(url string, config ...*Config) *FetchPatch
Sets HTTP method to PATCH.
func (f *Fetch) Patch(url string, config ...*Config) *FetchDelete
Sets HTTP method to DELETE.
func (f *Fetch) Delete(url string, config ...*Config) *FetchHead
Sets HTTP method to HEAD.
func (f *Fetch) Head(url string, config ...*Config) *FetchExecute
Executes the request.
func (f *Fetch) Execute() (*Response, error)Send
Alias for Execute.
func (f *Fetch) Send() (*Response, error)Retry
Retries the request with optional modifications.
func (f *Fetch) Retry(before func(f *Fetch)) (*Response, error)Clone
Creates a clone of the fetch instance.
func (f *Fetch) Clone() *FetchConfig
Returns the built configuration.
func (f *Fetch) Config() (*Config, error)Example
f := fetch.New()
f.SetBaseURL("https://api.example.com")
f.SetBearerToken("token")
f.SetTimeout(10 * time.Second)
response, err := f.Get("/users").Execute()
if err != nil {
panic(err)
}
fmt.Println(response.JSON())