Skip to content

Commit 89916d5

Browse files
Add docs from gofiber/fiber@f1b7f6d
1 parent 7e901ff commit 89916d5

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

docs/core/api/ctx.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,92 @@ func MyMiddleware() fiber.Handler {
420420
}
421421
```
422422

423+
### Matched
424+
425+
Returns `true` if the current request path was matched by the router.
426+
427+
```go title="Signature"
428+
func (c fiber.Ctx) Matched() bool
429+
```
430+
431+
```go title="Example"
432+
app.Use(func(c fiber.Ctx) error {
433+
if c.Matched() {
434+
return c.Next()
435+
}
436+
return c.Status(fiber.StatusNotFound).SendString("Not Found")
437+
})
438+
```
439+
440+
### IsMiddleware
441+
442+
Returns `true` if the current request handler was registered as middleware.
443+
444+
```go title="Signature"
445+
func (c fiber.Ctx) IsMiddleware() bool
446+
```
447+
448+
```go title="Example"
449+
app.Get("/route", func(c fiber.Ctx) error {
450+
fmt.Println(c.IsMiddleware()) // true
451+
return c.Next()
452+
}, func(c fiber.Ctx) error {
453+
fmt.Println(c.IsMiddleware()) // false
454+
return c.SendStatus(fiber.StatusOK)
455+
})
456+
```
457+
458+
### HasBody
459+
460+
Returns `true` if the incoming request contains a body or a `Content-Length` header greater than zero.
461+
462+
```go title="Signature"
463+
func (c fiber.Ctx) HasBody() bool
464+
```
465+
466+
```go title="Example"
467+
app.Post("/", func(c fiber.Ctx) error {
468+
if !c.HasBody() {
469+
return c.SendStatus(fiber.StatusBadRequest)
470+
}
471+
return c.SendString("OK")
472+
})
473+
```
474+
475+
### IsWebSocket
476+
477+
Returns `true` if the request includes a WebSocket upgrade handshake.
478+
479+
```go title="Signature"
480+
func (c fiber.Ctx) IsWebSocket() bool
481+
```
482+
483+
```go title="Example"
484+
app.Get("/", func(c fiber.Ctx) error {
485+
if c.IsWebSocket() {
486+
// handle websocket
487+
}
488+
return c.Next()
489+
})
490+
```
491+
492+
### IsPreflight
493+
494+
Returns `true` if the request is a CORS preflight (`OPTIONS` + `Access-Control-Request-Method` + `Origin`).
495+
496+
```go title="Signature"
497+
func (c fiber.Ctx) IsPreflight() bool
498+
```
499+
500+
```go title="Example"
501+
app.Use(func(c fiber.Ctx) error {
502+
if c.IsPreflight() {
503+
return c.SendStatus(fiber.StatusNoContent)
504+
}
505+
return c.Next()
506+
})
507+
```
508+
423509
### String
424510

425511
Returns a unique string representation of the context.

docs/core/whats_new.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,11 @@ testConfig := fiber.TestConfig{
490490
- **Drop**: Terminates the client connection silently without sending any HTTP headers or response body. This can be used for scenarios where you want to block certain requests without notifying the client, such as mitigating DDoS attacks or protecting sensitive endpoints from unauthorized access.
491491
- **End**: Similar to Express.js, immediately flushes the current response and closes the underlying connection.
492492
- **AcceptsLanguagesExtended**: Matches language ranges using RFC 4647 Extended Filtering with wildcard subtags.
493+
- **Matched**: Detects when the current request path matched a registered route.
494+
- **IsMiddleware**: Indicates if the current handler was registered as middleware.
495+
- **HasBody**: Quickly checks whether the request includes a body.
496+
- **IsWebSocket**: Reports if the request attempts a WebSocket upgrade.
497+
- **IsPreflight**: Identifies CORS preflight requests before handlers run.
493498

494499
### Removed Methods
495500

0 commit comments

Comments
 (0)