request

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package request provides the implementation of operations related to a http and cmd request it gives the features like reading parameters, bind request body, etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMock

func NewMock(method, target string, body io.Reader) (*http.Request, error)

NewMock returns a httptest request adds the mandatory headers to the request NOTE: Use only for tests

Types

type CMD

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

CMD (Command) is a data structure designed to access and manage various types of parameters, including path parameters, query parameters, and request bodies, used in command-line (cmd) applications.

It provides a simple and structured way to work with input parameters and data, making it easier to handle and process command-line input.

func (*CMD) Bind

func (c *CMD) Bind(i interface{}) error

Bind binds parameters to a struct.

func (*CMD) BindStrict

func (c *CMD) BindStrict(i interface{}) error

BindStrict is an alias for Bind.

func (*CMD) GetClaim

func (c *CMD) GetClaim(string) interface{}

GetClaim returns nil claim value for every request

func (*CMD) GetClaims

func (c *CMD) GetClaims() map[string]interface{}

GetClaims returns nil claims for every request

func (*CMD) Header

func (c *CMD) Header(key string) string

Header retrieves a parameter by key (same as Param).

func (*CMD) Param

func (c *CMD) Param(key string) string

Param retrieves a parameter by key.

func (*CMD) Params

func (c *CMD) Params() map[string]string

Params returns all parameters as a map.

func (*CMD) PathParam

func (c *CMD) PathParam(key string) string

PathParam retrieves a parameter by key (same as Param).

func (*CMD) Request

func (c *CMD) Request() *http.Request

Request returns a nil HTTP request.

type HTTP

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

HTTP represents a utility for accessing and performing HTTP operations, such as reading an HTTP request and gathering information related to the request, including path parameters.

The `HTTP` type provides a convenient interface for working with HTTP requests, extracting data from them, and processing related information. It is particularly useful for building applications that interact with the HTTP protocol and need to extract data from incoming requests.

func (*HTTP) Bind

func (h *HTTP) Bind(i interface{}) error

Bind checks the Content-Type to select a binding encoding automatically. Depending on the "Content-Type" header different bindings are used: - XML binding is used in case of: "application/xml" or "text/xml" - JSON binding is used by default It decodes the json payload into the type specified as a pointer. It returns an error if the decoding fails.

func (*HTTP) BindStrict

func (h *HTTP) BindStrict(i interface{}) error

BindStrict checks the "Content-Type" header to select a binding encoding automatically. Depending on the "Content-Type" header, different bindings are used: - XML binding is used in case of "application/xml" or "text/xml" content type. - JSON binding is used by default. It decodes the JSON or XML payload into the type specified as a pointer. It returns an error if the decoding fails, and it disallows unknown fields when decoding JSON payloads to enforce strict parsing.

func (*HTTP) Body

func (h *HTTP) Body() ([]byte, error)

Body returns the request body, throws an error if there was one

func (*HTTP) GetClaim

func (h *HTTP) GetClaim(claimKey string) interface{}

GetClaim function returns the value of claim key provided as the parameter

func (*HTTP) GetClaims

func (h *HTTP) GetClaims() map[string]interface{}

GetClaims function returns the map of claims

func (*HTTP) Header

func (h *HTTP) Header(key string) string

Header returns the value associated with the `key`, from the request headers

func (*HTTP) Method

func (h *HTTP) Method() string

Method returns the HTTP Method of the current request

func (*HTTP) Param

func (h *HTTP) Param(key string) string

Param returns the query parameter value for the given key, if any

func (*HTTP) ParamNames

func (h *HTTP) ParamNames() []string

ParamNames returns the list of query parameters (keys) for the current request

func (*HTTP) Params

func (h *HTTP) Params() map[string]string

Params returns the query parameters for the current request in the form of a mapping of key to it's values as comma separated values

func (*HTTP) PathParam

func (h *HTTP) PathParam(key string) string

PathParam returns the route values for the given key, if any

func (*HTTP) Request

func (h *HTTP) Request() *http.Request

Request returns the underlying HTTP request

func (*HTTP) String

func (h *HTTP) String() string

String satisfies the Stringer interface for the HTTP type

func (*HTTP) URI

func (h *HTTP) URI() string

URI returns the current HTTP requests URL-PATH

type Request

type Request interface {
	Request() *http.Request
	Params() map[string]string
	Param(string) string
	PathParam(string) string
	Bind(interface{}) error
	BindStrict(interface{}) error
	Header(string) string
	GetClaims() map[string]interface{}
	GetClaim(string) interface{}
}

Request provides the methods that are related to an incoming HTTP request

func NewCMDRequest

func NewCMDRequest() Request

NewCMDRequest creates a new CMD request instance.

func NewHTTPRequest

func NewHTTPRequest(r *http.Request) Request

NewHTTPRequest injects a *http.Request into a gofr.Request variable

Jump to

Keyboard shortcuts

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