@@ -49,6 +49,37 @@ app.Get("/", func(c *fiber.Ctx) error {
49
49
})
50
50
```
51
51
52
+ Media-Type parameters are supported.
53
+
54
+ ``` go title="Example 3"
55
+ // Accept: text/plain, application/json; version=1; foo=bar
56
+
57
+ app.Get (" /" , func (c *fiber.Ctx ) error {
58
+ // Extra parameters in the accept are ignored
59
+ c.Accepts (" text/plain;format=flowed" ) // "text/plain;format=flowed"
60
+
61
+ // An offer must contain all parameters present in the Accept type
62
+ c.Accepts (" application/json" ) // ""
63
+
64
+ // Parameter order and capitalization does not matter. Quotes on values are stripped.
65
+ c.Accepts (` application/json;foo="bar";VERSION=1` ) // "application/json;foo="bar";VERSION=1"
66
+ })
67
+ ```
68
+
69
+ ``` go title="Example 4"
70
+ // Accept: text/plain;format=flowed;q=0.9, text/plain
71
+ // i.e., "I prefer text/plain;format=flowed less than other forms of text/plain"
72
+ app.Get (" /" , func (c *fiber.Ctx ) error {
73
+ // Beware: the order in which offers are listed matters.
74
+ // Although the client specified they prefer not to receive format=flowed,
75
+ // the text/plain Accept matches with "text/plain;format=flowed" first, so it is returned.
76
+ c.Accepts (" text/plain;format=flowed" , " text/plain" ) // "text/plain;format=flowed"
77
+
78
+ // Here, things behave as expected:
79
+ c.Accepts (" text/plain" , " text/plain;format=flowed" ) // "text/plain"
80
+ })
81
+ ```
82
+
52
83
Fiber provides similar functions for the other accept headers.
53
84
54
85
``` go
@@ -583,7 +614,7 @@ app.Get("/", func(c *fiber.Ctx) error {
583
614
584
615
## GetReqHeaders
585
616
586
- Returns the HTTP request headers.
617
+ Returns the HTTP request headers as a map. Since a header can be set multiple times in a single request, the values of the map are slices of strings containing all the different values of the header .
587
618
588
619
``` go title="Signature"
589
620
func (c *Ctx ) GetReqHeaders () map [string ][]string
@@ -618,7 +649,7 @@ app.Get("/", func(c *fiber.Ctx) error {
618
649
619
650
## GetRespHeaders
620
651
621
- Returns the HTTP response headers.
652
+ Returns the HTTP response headers as a map. Since a header can be set multiple times in a single request, the values of the map are slices of strings containing all the different values of the header .
622
653
623
654
``` go title="Signature"
624
655
func (c *Ctx ) GetRespHeaders () map [string ][]string
@@ -766,11 +797,11 @@ app.Get("/", func(c *fiber.Ctx) error {
766
797
Converts any **interface** or **string** to JSON using the [encoding/json](https://pkg.go.dev/encoding/json) package.
767
798
768
799
:::info
769
- 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**.
770
801
:::
771
802
772
803
` ` ` go title=" Signature"
773
- func (c *Ctx) JSON (data interface {}) error
804
+ func (c *Ctx) JSON (data interface {}, ctype ... string ) error
774
805
` ` `
775
806
776
807
` ` ` go title=" Example"
@@ -796,6 +827,22 @@ app.Get("/json", func(c *fiber.Ctx) error {
796
827
})
797
828
// => Content-Type: application/json
798
829
// => "{"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
+ // => }"
799
846
})
800
847
` ` `
801
848
0 commit comments