Documentation
¶
Overview ¶
Package typeis matches Content-Type header values against a set of type candidates. It is a standard-library-only Go port of the npm "type-is" package, the content negotiation helper Express exposes as req.is and that body-parsing middleware uses to decide whether a request body is JSON, form data, or something else. The problem it solves is that a raw Content-Type header such as "application/json; charset=utf-8" is awkward to test directly: it carries parameters, is case-insensitive, and callers usually want to ask a convenience question like "is this JSON?" rather than compare strings.
The high-level entry point is Is, which takes a concrete Content-Type value and one or more candidate patterns and returns the matched candidate (in its full, normalized form) together with a boolean. Candidates may be written in several convenient shapes: a full type such as "application/json"; an extension-style shorthand such as "json", "html", "urlencoded", or "multipart"; a wildcard such as "*/*", "text/*", or "*/json"; or a structured "+suffix" match such as "+json" that matches any subtype ending in that suffix. When no candidates are supplied, Is simply reports whether the value is a non-empty, parseable type and echoes it back.
Internally each candidate is first expanded into a matchable pattern. A recognised shorthand is replaced by its full type from a small lookup table (for example "form" and "urlencoded" both become "application/x-www-form-urlencoded", and "multipart" becomes "multipart/*"); a leading "+suffix" becomes "*/*+suffix"; and a bare token containing no slash is treated as an extension and falls back to "application/<token>". The concrete value being tested is normalized by stripping any ";"-delimited parameters and lower-casing what remains, so the charset and boundary parameters that real headers carry never affect the match.
Match performs the actual comparison of an expected pattern against a concrete type. It supports "*" wildcards in either the type or the subtype position and understands the "type/subtype+suffix" structure: an expected "+json" matches "application/vnd.api+json" because the suffixes agree, while "*/*" matches anything. An empty value on either side never matches, and two non-parseable types (a missing or empty type or subtype) are treated as a non-match rather than an error.
Parity with the Node original covers the shorthand names, wildcard forms, and suffix matching that Express relies on for its own body parsers. The API shape is idiomatic Go: Is returns the matched pattern and an ok boolean instead of JavaScript's string-or-false union, and the package deliberately omits helpers that depend on a live request object (such as reading the body length header), leaving those to the surrounding framework.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Is ¶
Is matches a Content-Type value against one or more candidate types. Each candidate may be a full type ("application/json"), an extension shorthand ("json", "html", "urlencoded", "multipart"), a wildcard ("*/*", "text/*", "*/json") or a "+suffix" match ("+json"). It returns the matched candidate, normalized to its full type, and true. If nothing matches it returns "" and false.
Example ¶
ExampleIs tests a concrete Content-Type against a candidate and returns the matched type in its full, normalized form. The candidate "json" is an extension-style shorthand that expands to "application/json" before matching. The parameters on the actual header (here "; charset=utf-8") are stripped during normalization, so they never affect the result. On a match Is returns the expanded pattern and true; on no match it returns "" and false. This is the same convenience Express exposes as req.is.
package main
import (
"fmt"
"github.com/malcolmston/express/typeis"
)
func main() {
match, ok := typeis.Is("application/json; charset=utf-8", "json")
fmt.Println(match, ok)
}
Output: application/json true
Example (Multiple) ¶
ExampleIs_multiple tries several candidates in order and reports the first one that matches. Here the value is HTML, so the "json" candidate fails and the "html" candidate matches, returning its expanded form "text/html". Candidates may freely mix shorthands, full types, wildcards, and suffix forms. The first successful match wins and is returned normalized. This makes it easy to branch on the several content types a handler is willing to accept.
package main
import (
"fmt"
"github.com/malcolmston/express/typeis"
)
func main() {
match, ok := typeis.Is("text/html", "json", "html")
fmt.Println(match, ok)
}
Output: text/html true
func Match ¶
Match reports whether the actual (concrete) Content-Type matches the expected pattern. The expected pattern may use "*" wildcards for the type and/or subtype, and a "+suffix" on the subtype. Both arguments should be full types; parameters are ignored.
Example ¶
ExampleMatch compares an already-expanded pattern against a concrete type directly, with wildcard and suffix support. "application/*" matches any application subtype, and the suffix pattern "*/*+json" (the expanded form of the "+json" shorthand) matches "application/vnd.api+json" because the two suffixes agree. A pattern whose type does not match, such as "text/*" against an application type, returns false. Both arguments are normalized, so parameters are ignored. Match is the lower-level primitive that Is builds on after expanding shorthands.
package main
import (
"fmt"
"github.com/malcolmston/express/typeis"
)
func main() {
fmt.Println(typeis.Match("application/*", "application/json"))
fmt.Println(typeis.Match("*/*+json", "application/vnd.api+json"))
fmt.Println(typeis.Match("text/*", "application/json"))
}
Output: true true false
Types ¶
This section is empty.