You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
End immediately flushes the current response and closes the underlying connection.
490
+
491
+
```go title="Signature"
492
+
func(cfiber.Ctx) End() error
493
+
```
494
+
495
+
```go title="Example"
496
+
app.Get("/", func(c fiber.Ctx) error {
497
+
c.SendString("Hello World!")
498
+
return c.End()
499
+
})
500
+
```
501
+
502
+
:::caution
503
+
Calling `c.End()` will disallow further writes to the underlying connection.
504
+
:::
505
+
506
+
End can be used to stop a middleware from modifying a response of a handler/other middleware down the method chain
507
+
when they regain control after calling `c.Next()`.
508
+
509
+
```go title="Example"
510
+
// Error Logging/Responding middleware
511
+
app.Use(func(c fiber.Ctx) error {
512
+
err:= c.Next()
513
+
514
+
// Log errors & write the error to the response
515
+
if err != nil {
516
+
log.Printf("Got error in middleware: %v", err)
517
+
return c.Writef("(got error %v)", err)
518
+
}
519
+
520
+
// No errors occured
521
+
returnnil
522
+
})
523
+
524
+
// Handler with simulated error
525
+
app.Get("/", func(c fiber.Ctx) error {
526
+
// Closes the connection instantly after writing from this handler
527
+
// and disallow further modification of its response
528
+
defer c.End()
529
+
530
+
c.SendString("Hello, ... I forgot what comes next!")
531
+
return errors.New("some error")
532
+
})
533
+
```
534
+
487
535
## Format
488
536
489
537
Performs content-negotiation on the [Accept](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept) HTTP header. It uses [Accepts](ctx.md#accepts) to select a proper format from the supplied offers. A default handler can be provided by setting the `MediaType` to `"default"`. If no offers match and no default is provided, a 406 (Not Acceptable) response is sent. The Content-Type is automatically set when a handler is selected.
-**String**: Similar to Express.js, converts a value to a string.
342
342
-**ViewBind**: Binds data to a view, replacing the old `Bind` method.
343
343
-**CBOR**: Introducing [CBOR](https://cbor.io/) binary encoding format for both request & response body. CBOR is a binary data serialization format which is both compact and efficient, making it ideal for use in web applications.
344
+
-**End**: Similar to Express.js, immediately flushes the current response and closes the underlying connection.
0 commit comments