Package http provides a general purpose HTTP binding for endpoints.
const ( ContextKeyRequestMethod contextKey = iota ContextKeyRequestURI ContextKeyRequestPath ContextKeyRequestProto ContextKeyRequestHost ContextKeyRequestRemoteAddr ContextKeyRequestXForwardedFor ContextKeyRequestXForwardedProto ContextKeyRequestAuthorization ContextKeyRequestReferer ContextKeyRequestUserAgent ContextKeyRequestXRequestID ContextKeyRequestAccept ContextKeyResponseHeaders ContextKeyResponseSize )
This section is empty.
DefaultErrorEncoder writes the error to the ResponseWriter, by default a content type of text/plain, a body of the plain text of the 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 json.Marshaler, and the marshaling succeeds, a content type of application/json and the JSON encoded form of the error will be used. If the error implements StatusCoder, the provided StatusCode will be used instead of 500.
EncodeJSONRequest is an EncodeRequestFunc that serializes the request as a JSON object to the Request body. Many JSON-over-HTTP services can use it as a sensible default. If the request implements Headerer, the provided headers will be applied to the request.
EncodeJSONResponse is a EncodeResponseFunc that serializes the response as a JSON object to the ResponseWriter. Many JSON-over-HTTP services can use it as a sensible default. If the response implements Headerer, the provided headers will be applied to the response. If the response implements StatusCoder, the provided StatusCode will be used instead of 200.
EncodeXMLRequest is an EncodeRequestFunc that serializes the request as a XML object to the Request body. If the request implements Headerer, the provided headers will be applied to the request.
NopRequestDecoder is a DecodeRequestFunc that can be used for requests that do not need to be decoded, and simply returns nil, nil.
PopulateRequestContext is a RequestFunc that populates several values into the context from the HTTP request. Those values may be extracted using the corresponding ContextKey type in this package.
handler := NewServer( func(ctx context.Context, request interface{}) (response interface{}, err error) { fmt.Println("Method", ctx.Value(ContextKeyRequestMethod).(string)) fmt.Println("RequestPath", ctx.Value(ContextKeyRequestPath).(string)) fmt.Println("RequestURI", ctx.Value(ContextKeyRequestURI).(string)) fmt.Println("X-Request-ID", ctx.Value(ContextKeyRequestXRequestID).(string)) return struct{}{}, nil }, func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil }, func(context.Context, http.ResponseWriter, interface{}) error { return nil }, ServerBefore(PopulateRequestContext), ) server := httptest.NewServer(handler) defer server.Close() req, _ := http.NewRequest("PATCH", fmt.Sprintf("%s/search?q=sympatico", server.URL), nil) req.Header.Set("X-Request-Id", "a1b2c3d4e5") http.DefaultClient.Do(req)
Output: Method PATCH RequestPath /search RequestURI /search?q=sympatico X-Request-ID a1b2c3d4e5
Client wraps a URL and provides a method that implements endpoint.Endpoint.
NewClient constructs a usable Client for a single remote method.
NewExplicitClient is like NewClient but uses a CreateRequestFunc instead of a method, target URL, and EncodeRequestFunc, which allows for more control over the outgoing HTTP request.
Endpoint returns a usable Go kit endpoint that calls the remote HTTP endpoint.
ClientFinalizerFunc can be used to perform work at the end of a client HTTP request, after the response is returned. The principal intended use is for error logging. Additional response parameters are provided in the context under keys with the ContextKeyResponse prefix. Note: err may be nil. There maybe also no additional response parameters depending on when an error occurs.
type ClientOption func(*Client)
ClientOption sets an optional parameter for clients.
BufferedStream sets whether the HTTP response body is left open, allowing it to be read from later. Useful for transporting a file as a buffered stream. That body has to be drained and closed to properly end the request.
ClientAfter adds one or more ClientResponseFuncs, which are applied to the incoming HTTP response prior to it being decoded. This is useful for obtaining anything off of the response and adding it into the context prior to decoding.
ClientBefore adds one or more RequestFuncs to be applied to the outgoing HTTP request before it's invoked.
ClientFinalizer adds one or more ClientFinalizerFuncs to be executed at the end of every HTTP request. Finalizers are executed in the order in which they were added. By default, no finalizer is registered.
SetClient sets the underlying HTTP client used for requests. By default, http.DefaultClient is used.
ClientResponseFunc may take information from an HTTP request and make the response available for consumption. ClientResponseFuncs are only executed in clients, after a request has been made, but prior to it being decoded.
CreateRequestFunc creates an outgoing HTTP request based on the passed request object. It's designed to be used in HTTP clients, for client-side endpoints. It's a more powerful version of EncodeRequestFunc, and can be used if more fine-grained control of the HTTP request is required.
DecodeRequestFunc extracts a user-domain request object from an HTTP request object. It's designed to be used in HTTP servers, for server-side endpoints. One straightforward DecodeRequestFunc could be something that JSON decodes from the request body to the concrete request type.
DecodeResponseFunc extracts a user-domain response object from an HTTP response object. It's designed to be used in HTTP clients, for client-side endpoints. One straightforward DecodeResponseFunc could be something that JSON decodes from the response body to the concrete response type.
EncodeRequestFunc encodes the passed request object into the HTTP request object. It's designed to be used in HTTP clients, for client-side endpoints. One straightforward EncodeRequestFunc could be something that JSON encodes the object directly to the request body.
EncodeResponseFunc encodes the passed response object to the HTTP response writer. It's designed to be used in HTTP servers, for server-side endpoints. One straightforward EncodeResponseFunc could be something that JSON encodes the object directly to the response body.
ErrorEncoder is responsible for encoding an error to the ResponseWriter. Users are encouraged to use custom ErrorEncoders to encode HTTP errors to their clients, and will likely want to pass and check for their own error types. See the example shipping/handling service.
HTTPClient is an interface that models *http.Client.
type Headerer interface { }
Headerer is checked by DefaultErrorEncoder. If an error value implements Headerer, the provided headers will be applied to the response writer, after the Content-Type is set.
RequestFunc may take information from an HTTP request and put it into a request context. In Servers, RequestFuncs are executed prior to invoking the endpoint. In Clients, RequestFuncs are executed after creating the request but prior to invoking the HTTP client.
SetRequestHeader returns a RequestFunc that sets the given header.
Server wraps an endpoint and implements http.Handler.
NewServer constructs a new server, which implements http.Handler and wraps the provided endpoint.
ServeHTTP implements http.Handler.
ServerFinalizerFunc can be used to perform work at the end of an HTTP request, after the response has been written to the client. The principal intended use is for request logging. In addition to the response code provided in the function signature, additional response parameters are provided in the context under keys with the ContextKeyResponse prefix.
type ServerOption func(*Server)
ServerOption sets an optional parameter for servers.
ServerAfter functions are executed on the HTTP response writer after the endpoint is invoked, but before anything is written to the client.
ServerBefore functions are executed on the HTTP request object before the request is decoded.
ServerErrorEncoder is used to encode errors to the http.ResponseWriter whenever they're encountered in the processing of a request. Clients can use this to provide custom error formatting and response codes. By default, errors will be written with the DefaultErrorEncoder.
func ServerErrorHandler ΒΆ added in v0.9.0ServerErrorHandler is used to handle non-terminal errors. By default, non-terminal errors are ignored. This is intended as a diagnostic measure. Finer-grained control of error handling, including logging in more detail, should be performed in a custom ServerErrorEncoder or ServerFinalizer, both of which have access to the context.
ServerErrorLogger is used to log non-terminal errors. By default, no errors are logged. This is intended as a diagnostic measure. Finer-grained control of error handling, including logging in more detail, should be performed in a custom ServerErrorEncoder or ServerFinalizer, both of which have access to the context. Deprecated: Use ServerErrorHandler instead.
ServerFinalizer is executed at the end of every HTTP request. By default, no finalizer is registered.
ServerResponseFunc may take information from a request context and use it to manipulate a ResponseWriter. ServerResponseFuncs are only executed in servers, after invoking the endpoint but prior to writing a response.
SetContentType returns a ServerResponseFunc that sets the Content-Type header to the provided value.
SetResponseHeader returns a ServerResponseFunc that sets the given header.
type StatusCoder interface { StatusCode() int }
StatusCoder is checked by DefaultErrorEncoder. If an error value implements StatusCoder, the StatusCode will be used when encoding the error. By default, StatusInternalServerError (500) is used.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4