Documentation ¶
Index ¶
- Constants
- func CookiesToContext(ctx context.Context, r *http.Request) context.Context
- func EncodeHTTPGenericResponse(_ context.Context, w http.ResponseWriter, response interface{}) error
- func ErrorEncoder(_ context.Context, err error, w http.ResponseWriter)
- func HeadersToContext(ctx context.Context, r *http.Request) context.Context
- func HeadersToContextExcluding(ctx context.Context, r *http.Request, excludeHeaders []string) context.Context
- func NewBadRequestError(format string, a ...interface{}) error
- func UnmarshalJSON(b []byte, m proto.Message) error
- type HttpError
Constants ¶
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 ¶
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 ¶
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 ¶
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'"}
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 ¶
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 ¶
Error implements the error interface, so the HttpError could be used like any error.
func (HttpError) MarshalJSON ¶
MarshalJSON encodes payload property as JSON message and returns it. The encoded payload is the message that is returned as body from the ErrorEncoder.