request

package module
v1.10.4 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2023 License: MIT Imports: 18 Imported by: 0

README

Request

request is an intuitive and flexible HTTP client written in Go. Inspired by the simplicity of the project req, this library aims to provide a higher level of abstraction for handling HTTP requests and responses. With a wide range of customization options, it makes sending HTTP requests and handling responses simpler and more efficient.

Features

  • Support for all basic HTTP methods: GET, POST, PATCH, PUT, DELETE.
  • Built-in JSON and form data support for request bodies.
  • Cookie jar support for easier cookie management.
  • Dynamic and easy setting of query parameters and headers.
  • Integrated basic authentication.
  • Customizable timeouts and transport settings.
  • Context support for cancellable and timeout requests.
  • String, byte array, and io.Reader support for request bodies.
  • Helper methods for response handling: read as string, read all, convert to JSON, save to file.

Example Usage

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/faceair/request"
)

func main() {
	client := request.New()

	client.SetBaseURL("https://api.github.com").
		SetBaseHeaders(request.Headers{
			"Accept": "application/vnd.github.v3+json",
		})

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := client.Get(ctx, "/users/octocat")
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Println("Response:", resp.String())
}

This will send a GET request to https://api.github.com/users/octocat with the Accept header set as application/vnd.github.v3+json and a timeout of 5 seconds.

License

MIT.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	GZip = GZipOptions{}
)

Functions

func BodyJSON

func BodyJSON(v any) *bodyJSON

Types

type Client

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

func New

func New() *Client

func (*Client) Delete

func (r *Client) Delete(ctx context.Context, uri string, params ...any) (*Resp, error)

func (*Client) Do

func (r *Client) Do(ctx context.Context, method, uri string, params ...any) (*Resp, error)

func (*Client) EnableHTTPBalance added in v1.9.1

func (r *Client) EnableHTTPBalance(cacheExpire time.Duration) *Client

func (*Client) Get

func (r *Client) Get(ctx context.Context, uri string, params ...any) (*Resp, error)

func (*Client) Patch

func (r *Client) Patch(ctx context.Context, uri string, params ...any) (*Resp, error)

func (*Client) Post

func (r *Client) Post(ctx context.Context, uri string, params ...any) (*Resp, error)

func (*Client) Put

func (r *Client) Put(ctx context.Context, uri string, params ...any) (*Resp, error)

func (*Client) SetBaseClient

func (r *Client) SetBaseClient(client HTTPClient) *Client

func (*Client) SetBaseHeaders

func (r *Client) SetBaseHeaders(headers Headers) *Client

func (*Client) SetBaseURL

func (r *Client) SetBaseURL(baseURL string) *Client

func (*Client) SetBaseURLs added in v1.8.0

func (r *Client) SetBaseURLs(baseURLs []string) *Client

func (*Client) SetBasicAuth

func (r *Client) SetBasicAuth(username, password string) *Client

func (*Client) SetDialTimeout added in v1.9.7

func (r *Client) SetDialTimeout(timeout time.Duration) *Client

func (*Client) SetMaxIdleConns added in v1.9.8

func (r *Client) SetMaxIdleConns(maxIdleConns int) *Client

func (*Client) SetTimeout

func (r *Client) SetTimeout(timeout time.Duration) *Client

type DNSBalancer added in v1.9.1

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

func (*DNSBalancer) DialContext added in v1.9.1

func (lb *DNSBalancer) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

type DialContext added in v1.9.0

type DialContext func(ctx context.Context, network, addr string) (net.Conn, error)

type GZipOptions added in v1.10.1

type GZipOptions struct{}

type GetBody added in v1.8.2

type GetBody func() (io.ReadCloser, error)

type HTTPBalancer added in v1.9.1

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

func (*HTTPBalancer) Do added in v1.9.1

func (lb *HTTPBalancer) Do(req *http.Request) (*http.Response, error)

type HTTPClient

type HTTPClient interface {
	Do(*http.Request) (*http.Response, error)
}

type Headers

type Headers map[string]string

type MapForm

type MapForm map[string]string

type MapJSON

type MapJSON map[string]any

type MapMultipartForm added in v1.10.0

type MapMultipartForm map[string]any

type Query

type Query map[string]string

type Resp

type Resp struct {
	*http.Response
}

func Delete

func Delete(ctx context.Context, uri string, params ...interface{}) (*Resp, error)

func Get

func Get(ctx context.Context, uri string, params ...interface{}) (*Resp, error)

func Patch

func Patch(ctx context.Context, uri string, params ...interface{}) (*Resp, error)

func Post

func Post(ctx context.Context, uri string, params ...interface{}) (*Resp, error)

func Put

func Put(ctx context.Context, uri string, params ...interface{}) (*Resp, error)

func (*Resp) ReadAll

func (r *Resp) ReadAll() ([]byte, error)

func (*Resp) String

func (r *Resp) String() string

func (*Resp) ToFile

func (r *Resp) ToFile(filename string) error

func (*Resp) ToJSON

func (r *Resp) ToJSON(v any) error

Jump to

Keyboard shortcuts

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