Skip to main content

header

header is a directive executor who decodes a field from HTTP headers, i.e. http.Request.Header.

Definition

Name: "header"
Args: KEY1 [,KEY2 [,KEY3, ...]]

header will examine values of the keys one by one (KEY1 -> KEY2 -> ...) from the HTTP headers, the first non-empty value will be used to set the corresponding field of the input struct.

caution

The keys will be normalized, see CanonicalHeaderKey, e.g. the header key of Content-Type and content-type are the same, which works like case-insensitive.

Usage

type TokenInput struct {
Token string `in:"header=x-api-token,Authorization"`
}
Request (Header)TokenInput
GET /users HTTP/1.1

Host: foo.example
X-API-Token: abc
{ Token: "abc" }
GET /users HTTP/1.1

Host: foo.example
x-api-token: abc
{ Token: "abc" }
GET /users HTTP/1.1

Host: foo.example
Authorization: good
{ Token: "good" }
GET /users HTTP/1.1

Host: foo.example
X-Api-Token: apple
Authorization: banana
{ Token: "apple" }

// Check order: X-Api-Token -> Authorization