Header
To use headers from your request, you can use the Header
parameter.
Headers are always lowercased strings.
ts
import type { Header } from '@kitajs/runtime';
export function get(token: Header) {
return `You used the header 'Token: ${token}'!`;
}
1
json
{
"paths": {
"/": {
"get": {
"operationId": "getIndex",
"parameters": [
{
"schema": { "type": "string" },
"in": "header",
"name": "token",
"required": true
}
],
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "string" } }
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Custom names
You can also use custom names for the header you want to access.
ts
import type { Header } from '@kitajs/runtime';
// Access the header named 'Auth-Token'
export function get(anything: Header<'auth-token'>) {
return `This is your Auth-Token: ${anything}`;
}
1
json
{
"paths": {
"/": {
"get": {
"operationId": "getIndex",
"parameters": [
{
"schema": { "type": "string" },
"in": "header",
"name": "auth-token",
"required": true
}
],
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "string" } }
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Default values
Default values can be used with Header
.
ts
import type { Header } from '@kitajs/runtime';
export function get(token: Header = 'please let me in') {
return `You used the header 'Token: ${token}'!`;
}
1
json
{
"paths": {
"/": {
"get": {
"operationId": "getIndex",
"parameters": [
{
"schema": { "type": "string" },
"in": "header",
"name": "token",
"required": false
}
],
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "string" } }
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25