request

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 10 Imported by: 0

README

go-request

go-request is a wrapper for net/http request to make it easier to send HTTP request.

Install

go get -u github.com/amuwall/go-request

Quick Start

// Create client, default scheme is https and port is 443
client, err := request.NewClient("127.0.0.1")

// Create request
req, err := request.NewRequest(http.MethodPost, "/api/test")

// Send request
resp, err := client.do(req)

Advanced

Client Options

Here are some client options. When call NewClient, you can use these to set client.

  • WithScheme
  • WithPort
  • WithTransport
  • WithTimeout
  • WithClientCertificateBlock
  • WithClientCertificateFile
  • WithTLSServerName
  • WithSkipVerifyCertificates

Example:

client, err := request.NewClient(
    "127.0.0.1",
    request.WithScheme("http"),
    request.WithScheme("80"),
)
Request Options

Here are some request options. When call NewRequest, you can use these to set request.

  • WithHost
  • WithHeaders
  • WithQueryParams
  • WithBodyParams

Example:

req, err := request.NewRequest(
    http.MethodPost,
    "/api/test",
    request.WithBodyParams(
        request.NewJsonBodyParams(map[string]string{"msg": "hello"}),
    ),
)
Request Body Params

Here are two defined params, JsonBodyParams and FormBodyParams.

JsonBodyParams
params := request.NewJsonBodyParams(map[string]string{"msg": "hello"})
FormBodyParams
params := request.NewFormBodyParams(map[string]string{"msg": "hello"})
params.AddFile("file", "test.txt", f) // Send file
Response

You can use .StatusCode to get response status code, use .Header to get response header, and use .RawBody to get response body.
If your response body is JSON, you can call .UnmarshalJSONBody method to unmarshal response body

err := resp.UnmarshalJSONBody(val)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BodyParams

type BodyParams interface {
	Build() (contentType string, body io.Reader, err error)
}

type Client

type Client struct {
	Scheme string
	Host   string
	Port   uint16
	// contains filtered or unexported fields
}

func NewClient

func NewClient(host string, options ...ClientOption) (client *Client, err error)

func (*Client) BaseURL

func (c *Client) BaseURL() string

func (*Client) Do

func (c *Client) Do(req *Request) (*Response, error)

type ClientOption

type ClientOption func(*Client) error

func WithClientCertificateBlock

func WithClientCertificateBlock(clientCrtBlock, clientKeyBlock []byte) ClientOption

func WithClientCertificateFile

func WithClientCertificateFile(clientCrtFile, clientKeyFile string) ClientOption

func WithPort

func WithPort(port uint16) ClientOption

func WithScheme

func WithScheme(scheme string) ClientOption

func WithSkipVerifyCertificates

func WithSkipVerifyCertificates() ClientOption

func WithTLSServerName

func WithTLSServerName(serverName string) ClientOption

func WithTimeout

func WithTimeout(timeout time.Duration) ClientOption

func WithTransport

func WithTransport(transport *http.Transport) ClientOption

type FormBodyParams

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

func NewFormBodyParams

func NewFormBodyParams(params map[string]string) *FormBodyParams

func (*FormBodyParams) AddFile

func (p *FormBodyParams) AddFile(fieldName, fileName string, reader io.Reader)

func (*FormBodyParams) Build

func (p *FormBodyParams) Build() (contentType string, body io.Reader, err error)

type JsonBodyParams

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

func NewJsonBodyParams

func NewJsonBodyParams(params interface{}) *JsonBodyParams

func (*JsonBodyParams) Build

func (p *JsonBodyParams) Build() (contentType string, body io.Reader, err error)

type QueryParams

type QueryParams map[string][]string

func NewQueryParams

func NewQueryParams(params map[string]string) QueryParams

func (QueryParams) Add

func (p QueryParams) Add(key, value string)

func (QueryParams) Encode

func (p QueryParams) Encode() string

func (QueryParams) Get

func (p QueryParams) Get(key string) string

func (QueryParams) Set

func (p QueryParams) Set(key, value string)

type Request

type Request struct {
	Method string
	Path   string

	Host    string
	Headers http.Header

	QueryParams QueryParams
	BodyParams  BodyParams
}

func NewRequest

func NewRequest(method, path string, options ...RequestOption) (request *Request, err error)

type RequestOption

type RequestOption func(*Request) error

func WithBodyParams

func WithBodyParams(bodyParams BodyParams) RequestOption

func WithHeaders

func WithHeaders(headers map[string]string) RequestOption

func WithHost

func WithHost(host string) RequestOption

func WithQueryParams

func WithQueryParams(queryParams QueryParams) RequestOption

type Response

type Response struct {
	StatusCode int
	Header     http.Header
	RawBody    []byte
}

func (*Response) UnmarshalJSONBody

func (resp *Response) UnmarshalJSONBody(val interface{}) (err error)

Jump to

Keyboard shortcuts

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