com_http

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, method, reqURL string, body []byte, options ...Option) (*http.Response, error)

Do send an HTTP request and returns an HTTP response

func Get

func Get(ctx context.Context, reqURL string, options ...Option) (*http.Response, error)

Get issues a GET to the specified URL.

func Post

func Post(ctx context.Context, reqURL string, body []byte, options ...Option) (*http.Response, error)

Post issues a POST to the specified URL.

func PostForm

func PostForm(ctx context.Context, reqURL string, data url.Values, options ...Option) (*http.Response, error)

PostForm issues a POST to the specified URL, with data's keys and values URL-encoded as the request body.

func Upload

func Upload(ctx context.Context, reqURL string, form UploadForm, options ...Option) (*http.Response, error)

Upload issues a UPLOAD to the specified URL.

Types

type FormFileFunc

type FormFileFunc func(w io.Writer) error

FormFileFunc writes file content to multipart writer.

type HTTPClient

type HTTPClient interface {
	// Do send an HTTP request and returns an HTTP response.
	// Should use context to specify the timeout for request.
	Do(ctx context.Context, method, reqURL string, body []byte, options ...Option) (*http.Response, error)

	// Upload issues a UPLOAD to the specified URL.
	// Should use context to specify the timeout for request.
	Upload(ctx context.Context, reqURL string, form UploadForm, options ...Option) (*http.Response, error)
}

HTTPClient is the interface for a http client.

func NewHTTPClient

func NewHTTPClient(client *http.Client) HTTPClient

NewHTTPClient returns a new http client

type Option

type Option func(s *config)

Option configures how we set up the http request.

func WithClose

func WithClose() Option

WithClose specifies close the connection after replying to this request (for servers) or after sending this request and reading its response (for clients).

func WithCookies

func WithCookies(cookies ...*http.Cookie) Option

WithCookies specifies the cookies to http request.

func WithHeader

func WithHeader(key, value string) Option

WithHeader specifies the header to http request.

type UploadField

type UploadField func(f *uploadForm)

UploadField configures how we set up the upload from.

func WithFormField

func WithFormField(fieldName, fieldValue string) UploadField

WithFormField specifies the form field to upload from.

func WithFormFile

func WithFormFile(fieldName, fileName string, fn FormFileFunc) UploadField

WithFormFile specifies the file field to upload from.

type UploadForm

type UploadForm interface {
	// Write writes fields to multipart writer
	Write(w *multipart.Writer) error
}

UploadForm is the interface for http upload.

func NewUploadForm

func NewUploadForm(fields ...UploadField) UploadForm

NewUploadForm returns an upload form

Jump to

Keyboard shortcuts

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