request

package module
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2023 License: MIT Imports: 16 Imported by: 1

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

This section is empty.

Functions

func BodyJSON

func BodyJSON(v interface{}) *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 ...interface{}) (*Resp, error)

func (*Client) Do

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

func (*Client) Get

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

func (*Client) Patch

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

func (*Client) Post

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

func (*Client) Put

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

func (*Client) SetBaseClient

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

func (*Client) SetBaseHeaders

func (r *Client) SetBaseHeaders(headers map[string]string) *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) SetTimeout

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

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]interface{}

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

Jump to

Keyboard shortcuts

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