httputils

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 29, 2022 License: Apache-2.0 Imports: 8 Imported by: 3

Documentation

Index

Constants

View Source
const (
	ContentType               = "Content-Type"
	AccessControlAllowOrigin  = "Access-Control-Allow-Origin"
	AccessControlAllowHeaders = "Access-Control-Allow-Headers"
	AccessControlAllowMethods = "Access-Control-Allow-Methods"
)

collection of http headers

View Source
const (
	ApplicationJSON     = "application/json"
	TextPlain           = "text/plain"
	ApplicationActuator = "application/vnd.spring-boot.actuator.v3+json"
)

collection of common content-type values

Variables

View Source
var (
	// IsHTTPInformation returns true if a status code is of the 1xx range (100 - 199).
	IsHTTPInformation = statusGroupEqual(statusCodeGroupInfo)

	// IsHTTPSuccess returns true if a status code is of the 2xx range (200 - 299).
	IsHTTPSuccess = statusGroupEqual(statusCodeGroupSuccess)

	// IsHTTPRedirect returns true if a status code is of the 3xx range (300 - 399).
	IsHTTPRedirect = statusGroupEqual(statusCodeGroupRedirect)

	// IsHTTPClientError returns true if a status code is of the 4xx range (400 - 499).
	IsHTTPClientError = statusGroupEqual(statusCodeGroupClientErr)

	// IsHTTPServerError returns true if a status code is of the 5xx range (500 - 599).
	IsHTTPServerError = statusGroupEqual(statusCodeGroupServerErr)
)

Functions

func HeadersAsFlatMap

func HeadersAsFlatMap(h *http.Header) map[string]string

HeadersAsFlatMap converts a http.Header into a map, flattening duplicate values e.g. X-My-Header=a,X-My-Header=b -> {X-My-Header: a}

func HeadersAsMap

func HeadersAsMap(h *http.Header) map[string][]string

HeadersAsMap converts a http.Header into a map

func IsHTTPError

func IsHTTPError(code int) bool

IsHTTPError returns true if a status code is 4xx or 5xx.

func RemoveHeaders

func RemoveHeaders(h *http.Header, keys []string)

RemoveHeaders removes all matching headers from a http.Header

func ReturnJSON

func ReturnJSON(ctx context.Context, w http.ResponseWriter, code int, v interface{})

ReturnJSON converts a given interface into JSON and writes it into a http.ResponseWriter.

You should not write any additional data to the http.ResponseWriter after this

func ReturnProtoJSON

func ReturnProtoJSON(ctx context.Context, w http.ResponseWriter, code int, v proto.Message)

ReturnProtoJSON converts a given proto.Message into JSON and writes it into an HTTP response.

You should not write any additional data to the http.ResponseWriter after this.

func WithBody

func WithBody(r *http.Request, v interface{}) error

WithBody reads an HTTP JSON request body and marshals it into a given struct Param v must be a pointer

func WithProtoBody

func WithProtoBody(r *http.Request, v proto.Message) error

WithProtoBody reads an HTTP JSON request body and marshals it into a given proto.Message

Types

This section is empty.

Jump to

Keyboard shortcuts

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