A go library for parsing and using the accept header
| go.mod | change module path | |
| LICENSE | Initial commit | |
| match.go | return first servable for empty header | |
| match_test.go | also match application/json to application/ld+json | |
| README.md | correct and explain example | |
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"funcHandleRequest(whttp.ResponseWriter,r*http.Request){// we're using a slice instead of a map[string]func,// because we want to be able to rank the typespossible_types:=[]string{"text/html","application/ld+json","text/plain",}switchaccept.ServeType(possible_types,r.Header.Get("Accept")){case"text/html":// write htmlcase"application/ld+json":// also matches if request accepts 'application/json'// write jsonldcase"text/plain":// write plaintextdefault:// only if accept header exists and none matches one of the possible typeshttp.Error(w,"Could not serve requested Type",http.StatusNotAcceptable)}}Example:
codeberg.org/uonel/useragent