accept

package module
v0.0.0-...-1ce6977 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2022 License: MIT Imports: 5 Imported by: 0

README

Accept

A go library to parse the Accept HTTP Header

It was written because of this toot: https://uwu.social/@Joshix/108424918060682309

Usage:
import "codeberg.org/u0nel/accept"

func HandleRequest(w http.ResponseWriter, r *http.Request) {
	// we're using a slice instead of a map[string]func,
	// because we want to be able to rank the types
	possible_types := []string{
				"text/html",
				"application/json+ld",
				"text/plain",
				}
	switch accept.ServeType(possible_types, r.Header.Get("Accept")) {
	case "text/html":
		// write html
	case "application/json+ld":
		// write jsonld
	case "text/plain":
		// write plaintext
	default:
		// only if accept header exists and none matches one of the possible types
		http.Error(w, "Could not serve requested Type", http.StatusNotAcceptable)
	}
}

Example:
codeberg.org/uonel/useragent

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Matches

func Matches(matcher string, absolute string) bool

func ServeType

func ServeType(servable []string, acceptheader string) string

func Sort

func Sort(items []AcceptExt)

Types

type AcceptExt

type AcceptExt struct {
	MediaRange string
	Params     map[string]string
}

func ParseHeader

func ParseHeader(header string) (accepts []AcceptExt)

Jump to

Keyboard shortcuts

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