contenttype

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package contenttype validates the Content-Type header.

Index

Constants

View Source
const (
	ContentTypeJSON        = "application/json"
	ContentTypeFormEncoded = "application/x-www-form-urlencoded"
	ContentTypeFormData    = "multipart/form-data"
)

Some commonly used Content-Type values.

Variables

View Source
var DefaultOptions = map[string]Options{
	"": Options{
		Methods:           []string{http.MethodPost, http.MethodPut, http.MethodDelete},
		ValidContentTypes: []string{ContentTypeJSON, ContentTypeFormEncoded, ContentTypeFormData},
		AllowEmpty:        false,
	},
}

DefaultOptions for the Valid() middleware if options if nil.

View Source
var JSON = map[string]Options{
	"": Options{
		Methods:           []string{},
		ValidContentTypes: []string{ContentTypeJSON},
		AllowEmpty:        false,
	},
}

JSON are options to allow only JSON for all requests verbs (GET included).

Functions

func Validate

func Validate(opts map[string]Options) func(http.Handler) http.Handler

Validate ensures that the content type header is set and is one of the allowed content types. This ONLY applies to POST, PUT, and DELETE requests.

The option key is the pathname to check; use an empty string for the default options (this is mandatory).

Types

type Options

type Options struct {
	// Methods to validate; if the request method isn't in the list we won't do
	// anything.
	Methods []string

	// List of valid content-types.
	ValidContentTypes []string

	// AllowEmpty indicates if not sending a Content-Type header is allowed.
	AllowEmpty bool
}

Options for Validate().

Jump to

Keyboard shortcuts

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