request

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 License: MIT Imports: 8 Imported by: 0

README

HTTP Request

Example:

package main

import (
	"context"
	"fmt"
	"net/http"
	"net/url"

	"github.com/QisFj/godry/http/request"
)

func main() {
	ctx := context.Background()
	var req interface{}  // request body = json.Marshal(req)
	var resp interface{} // json.Unmarshal(response body, &resp)
	err := request.New().With(
		request.Options.Method(http.MethodPost),
		request.Options.CheckResponseBeforeUnmarshal(func(statusCode int, body []byte) error {
			if statusCode != http.StatusOK {
				return fmt.Errorf("http status: %d - %s", statusCode, http.StatusText(statusCode))
			}
			return nil
		}),
	).DoCtx(ctx, "http://example.com", url.Values{
		"params": {"1", "2"},
	}, req, &resp)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Resp: %#v\n", resp)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Options options

Functions

This section is empty.

Types

type CheckResponseAfterUnmarshalFunc

type CheckResponseAfterUnmarshalFunc func(statusCode int, v interface{}) error

type CheckResponseAfterUnmarshalFuncs

type CheckResponseAfterUnmarshalFuncs []CheckResponseAfterUnmarshalFunc

func (CheckResponseAfterUnmarshalFuncs) Check

func (fs CheckResponseAfterUnmarshalFuncs) Check(statusCode int, v interface{}) error

type CheckResponseBeforeUnmarshalFunc

type CheckResponseBeforeUnmarshalFunc func(statusCode int, body []byte) error

type CheckResponseBeforeUnmarshalFuncs

type CheckResponseBeforeUnmarshalFuncs []CheckResponseBeforeUnmarshalFunc

func (CheckResponseBeforeUnmarshalFuncs) Check

func (fs CheckResponseBeforeUnmarshalFuncs) Check(statusCode int, body []byte) error

type Codec

type Codec interface {
	Marshaller
	Unmarshaller
}

type CodecFuncs

type CodecFuncs struct {
	MarshalFunc
	UnmarshalFunc
}

type Log

type Log struct {
	Logger            func(format string, v ...interface{})
	CtxLogger         func(ctx context.Context, format string, v ...interface{}) // if this has been set, Logger make no sense
	URL               bool
	RequestBody       bool
	RequestBodyLimit  int // 0 means no limit
	ResponseBody      bool
	ResponseBodyLimit int // 0 means no limit
}

func (Log) LogRequestBody

func (log Log) LogRequestBody(ctx context.Context, body []byte)

func (Log) LogResponseBody

func (log Log) LogResponseBody(ctx context.Context, body []byte)

func (Log) LogURL

func (log Log) LogURL(ctx context.Context, m string, u *url.URL)

type MarshalFunc

type MarshalFunc func(v interface{}) ([]byte, error)

func MarshalFuncOf

func MarshalFuncOf(m Marshaller) MarshalFunc

func (MarshalFunc) Marshal

func (f MarshalFunc) Marshal(v interface{}) ([]byte, error)

type Marshaller

type Marshaller interface {
	Marshal(v interface{}) ([]byte, error)
}

type Option

type Option func(r *Request)

type Request

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

zero value is not ready for use, call New

func New

func New() *Request

func (Request) Clone added in v0.1.4

func (r Request) Clone() *Request

func (Request) Do

func (r Request) Do(u string, v url.Values, req, resp interface{}) error

Do request with default context

func (Request) DoCtx

func (r Request) DoCtx(ctx context.Context, u string, v url.Values, reqObj, respObj interface{}) (err error)

Do request

func (*Request) With

func (r *Request) With(opts ...Option) *Request

important: With changed the original Request

type RequestHookFunc added in v0.1.9

type RequestHookFunc func(ctx context.Context, req *http.Request) error

type RequestHookFuncs added in v0.1.9

type RequestHookFuncs []RequestHookFunc

func (RequestHookFuncs) Hook added in v0.1.9

func (fs RequestHookFuncs) Hook(ctx context.Context, req *http.Request) error

type ResponseHookFunc added in v0.1.9

type ResponseHookFunc func(ctx context.Context, resp *http.Response) error

type ResponseHookFuncs added in v0.1.9

type ResponseHookFuncs []ResponseHookFunc

func (ResponseHookFuncs) Hook added in v0.1.9

func (fs ResponseHookFuncs) Hook(ctx context.Context, resp *http.Response) error

type UnmarshalFunc

type UnmarshalFunc func(data []byte, v interface{}) error

func UnmarshalFuncOf

func UnmarshalFuncOf(u Unmarshaller) UnmarshalFunc

func (UnmarshalFunc) Unmarshal

func (f UnmarshalFunc) Unmarshal(data []byte, v interface{}) error

type Unmarshaller

type Unmarshaller interface {
	Unmarshal(data []byte, v interface{}) error
}

Jump to

Keyboard shortcuts

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