Skip to content

Commit 3ce6f21

Browse files
committed
chore: add a guide for Header
1 parent 840c8f4 commit 3ce6f21

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

docs/docs/guides/input/header.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Request Header
2+
3+
Django Ninja provides a convenient `Header()` parameter function to extract values from HTTP request headers in your API endpoints.
4+
5+
## Usage Patterns
6+
7+
There are two main ways to work with headers:
8+
9+
### 1. Custom Header Name
10+
11+
!!! note
12+
Use the `alias` parameter to specify the exact HTTP header name (with hyphens)
13+
Do NOT use underscores in the parameter name, as Python identifiers cannot contain hyphens.
14+
15+
- Python 3.6+ non-Annotated
16+
17+
```python hl_lines="5"
18+
{!./src/tutorial/header/code01.py!}
19+
```
20+
21+
- Python 3.9+ Annotated
22+
23+
```python hl_lines="5 9"
24+
{!./src/tutorial/header/code02.py!}
25+
```
26+
27+
This pattern is ideal for:
28+
- Custom headers (e.g., `X-API-Key`, `X-Request-ID`)
29+
- Non-standard header names
30+
- When you want explicit control over header mapping
31+
32+
### 2. Well-Known Headers
33+
34+
For common HTTP headers, you can use snake_case parameter names without an alias:
35+
36+
```python hl_lines="5 11 19 23"
37+
{!./src/tutorial/header/code03.py!}
38+
```
39+
40+
Common implicit headers include:
41+
42+
- `user_agent``User-Agent`
43+
- `content_length``Content-Length`
44+
- `content_type``Content-Type`
45+
- `authorization``Authorization`
46+
- etc.

docs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ nav:
5555
- guides/input/path-params.md
5656
- guides/input/query-params.md
5757
- guides/input/body.md
58+
- guides/input/header.md
5859
- guides/input/form-params.md
5960
- guides/input/file-params.md
6061
- guides/input/request-parsers.md

docs/src/tutorial/header/code01.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from ninja import Header
2+
3+
4+
@api.get("/events")
5+
def events(request, custom_header: str = Header(alias="X-Custom-Header")):
6+
return {"received": custom_header}

docs/src/tutorial/header/code02.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import Annotated
2+
from ninja import Header
3+
4+
5+
CustomHeader = Annotated[str, Header(alias="X-Custom-Header")]
6+
7+
8+
@api.get("/events")
9+
def events(request, custom_header: CustomHeader):
10+
return {"received": custom_header}

docs/src/tutorial/header/code03.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from ninja import Header
2+
3+
4+
@api.get("/events")
5+
def events(request, user_agent: str | None = Header(None)):
6+
return {"user_agent": user_agent}
7+
8+
9+
# Using alias instead of argument name
10+
@api.get("/events")
11+
def events(request, ua: str | None = Header(None, alias="User-Agent")):
12+
return {"user_agent": ua}
13+
14+
15+
# Python 3.9+ Annotated
16+
from typing import Annotated
17+
18+
19+
UserAgent = Annotated[str | None, Header(None)]
20+
21+
22+
@api.get("/events")
23+
def events(request, user_agent: UserAgent):
24+
return {"user_agent": user_agent}

0 commit comments

Comments
 (0)