Skip to content

Commit 20085ca

Browse files
Add docs from gofiber/fiber@9f082af
1 parent 03253a1 commit 20085ca

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

docs/core/api/ctx.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,11 +797,11 @@ app.Get("/", func(c *fiber.Ctx) error {
797797
Converts any **interface** or **string** to JSON using the [encoding/json](https://pkg.go.dev/encoding/json) package.
798798
799799
:::info
800-
JSON also sets the content header to **application/json**.
800+
JSON also sets the content header to the `ctype` parameter. If no `ctype` is passed in, the header is set to **application/json**.
801801
:::
802802
803803
```go title="Signature"
804-
func (c *Ctx) JSON(data interface{}) error
804+
func (c *Ctx) JSON(data interface{}, ctype ...string) error
805805
```
806806
807807
```go title="Example"
@@ -827,6 +827,22 @@ app.Get("/json", func(c *fiber.Ctx) error {
827827
})
828828
// => Content-Type: application/json
829829
// => "{"name": "Grame", "age": 20}"
830+
831+
return c.JSON(fiber.Map{
832+
"type": "https://example.com/probs/out-of-credit",
833+
"title": "You do not have enough credit.",
834+
"status": 403,
835+
"detail": "Your current balance is 30, but that costs 50.",
836+
"instance": "/account/12345/msgs/abc",
837+
}, "application/problem+json")
838+
// => Content-Type: application/problem+json
839+
// => "{
840+
// => "type": "https://example.com/probs/out-of-credit",
841+
// => "title": "You do not have enough credit.",
842+
// => "status": 403,
843+
// => "detail": "Your current balance is 30, but that costs 50.",
844+
// => "instance": "/account/12345/msgs/abc",
845+
// => }"
830846
})
831847
```
832848

0 commit comments

Comments
 (0)