contenttype

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowContentType

func AllowContentType(contentTypes []string, opts ...Option) middleware.MiddlewareFunc

AllowContentType creates middleware that validates if the request's Content-Type header is included in the specified list.

If the Content-Type header does not exist or has a media type not in the list, it returns a response with status code 415 (Unsupported Media Type) and "Unsupported Media Type" body by default. The response body can be customized with the WithResponse option.

If the contentTypes list is empty, all Content-Types will be rejected. Content-Type comparison is done only on the media type part, parameters (e.g., charset=utf-8) are ignored. Comparison is case-insensitive.

Examples: AllowContentType([]string{"application/json"}) allows "application/json" and "application/json; charset=utf-8". AllowContentType([]string{"application/json", "application/xml"}) allows both JSON and XML.

Types

type Config

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

Config is the configuration for the AllowContentType middleware.

type Option

type Option func(*Config)

Option is a function type to modify the AllowContentType configuration.

func WithResponse

func WithResponse(contentType string, body string) Option

WithResponse sets the response Content-Type header and response body for error cases.

Jump to

Keyboard shortcuts

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