httpkit

package
v0.0.0-...-5b9a9d8 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const JSONContentType = "application/json; charset=utf-8"

JSONContentType is the default value of JSON messages recognized by the HTTP clients.

Variables

This section is empty.

Functions

func CookiesToContext

func CookiesToContext(ctx context.Context, r *http.Request) context.Context

CookiesToContext appends all cookies from the request to Context.

func EncodeHTTPGenericResponse

func EncodeHTTPGenericResponse(_ context.Context, w http.ResponseWriter, response interface{}) error

EncodeHTTPGenericResponse is a transport/http.EncodeResponseFunc that encodes the response as JSON to the response writer. Primarily useful in a server.

func ErrorEncoder

func ErrorEncoder(_ context.Context, err error, w http.ResponseWriter)

ErrorEncoder writes the error to the ResponseWriter, by default a content type of application/json, a body of json with key "message" and the value error.Error(), and a status code of 500. If the error implements Headerer, the provided headers will be applied to the response. If the error implements grpc status.Error then the message and details will be encoded as json and will be encoded otherwise json.Marshaler, and the marshaling succeeds, the JSON encoded form of the error will be used. If the error implements StatusCoder, the provided StatusCode will be used instead of 500.

func HeadersToContext

func HeadersToContext(ctx context.Context, r *http.Request) context.Context

HeadersToContext adds all HTTP header values into the passed context.Context. The keys are added with request.ContextKey as and lookups should be performed by using the same type.

func HeadersToContextExcluding

func HeadersToContextExcluding(ctx context.Context, r *http.Request, excludeHeaders []string) context.Context

HeadersToContextExcluding adds all HTTP header values into the passed context.Context. The keys are added with request.ContextKey as and lookups should be performed by using the same type.

func NewBadRequestError

func NewBadRequestError(format string, a ...interface{}) error

NewBadRequestError is creating a status error for bad request by formatting the passed format and arguments into a messages. The response of this error will be:

{"message": "invalid json message '123'"}

func UnmarshalJSON

func UnmarshalJSON(b []byte, m proto.Message) error

UnmarshalJSON decodes the message bytes into a protobuf message.

Types

type HttpError

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

HttpError satisfies the Headerer and StatusCoder interfaces in package github.com/go-kit/kit/transport/http. It's used to return user defined Error objects in JSON format.

func NewHttpError

func NewHttpError(code int, payload interface{}, headers map[string][]string) *HttpError

NewHttpError creates an HTTP error with the given status code and headers. The headers could be nil or empty if you don't want such to be send back to the client.

func (HttpError) Error

func (h HttpError) Error() string

Error implements the error interface, so the HttpError could be used like any error.

func (HttpError) Headers

func (h HttpError) Headers() http.Header

func (HttpError) MarshalJSON

func (h HttpError) MarshalJSON() ([]byte, error)

MarshalJSON encodes payload property as JSON message and returns it. The encoded payload is the message that is returned as body from the ErrorEncoder.

func (HttpError) StatusCode

func (h HttpError) StatusCode() int

Jump to

Keyboard shortcuts

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