Skip to content

Commit d381170

Browse files
committed
update v2 with last release
1 parent 5f512a5 commit d381170

File tree

4 files changed

+192
-46
lines changed

4 files changed

+192
-46
lines changed

versioned_docs/version-v2.x/api/client.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,61 @@ agent.SetResponse(resp)
537537
ReleaseResponse(resp)
538538
```
539539

540+
<details><summary>Example handling for response values</summary>
541+
542+
```go title="Example handling response"
543+
// Create a Fiber HTTP client agent
544+
agent := fiber.Get("https://httpbin.org/get")
545+
546+
// Acquire a response object to store the result
547+
resp := fiber.AcquireResponse()
548+
agent.SetResponse(resp)
549+
550+
// Perform the HTTP GET request
551+
code, body, errs := agent.String()
552+
if errs != nil {
553+
// Handle any errors that occur during the request
554+
panic(errs)
555+
}
556+
557+
// Print the HTTP response code and body
558+
fmt.Println("Response Code:", code)
559+
fmt.Println("Response Body:", body)
560+
561+
// Visit and print all the headers in the response
562+
resp.Header.VisitAll(func(key, value []byte) {
563+
fmt.Println("Header", string(key), "value", string(value))
564+
})
565+
566+
// Release the response to free up resources
567+
fiber.ReleaseResponse(resp)
568+
```
569+
570+
Output:
571+
```txt title="Output"
572+
Response Code: 200
573+
Response Body: {
574+
"args": {},
575+
"headers": {
576+
"Host": "httpbin.org",
577+
"User-Agent": "fiber",
578+
"X-Amzn-Trace-Id": "Root=1-653763d0-2555d5ba3838f1e9092f9f72"
579+
},
580+
"origin": "83.137.191.1",
581+
"url": "https://httpbin.org/get"
582+
}
583+
584+
Header Content-Length value 226
585+
Header Content-Type value application/json
586+
Header Server value gunicorn/19.9.0
587+
Header Date value Tue, 24 Oct 2023 06:27:28 GMT
588+
Header Connection value keep-alive
589+
Header Access-Control-Allow-Origin value *
590+
Header Access-Control-Allow-Credentials value true
591+
```
592+
593+
</details>
594+
540595
### Dest
541596

542597
Dest sets custom dest. The contents of dest will be replaced by the response body, if the dest is too small a new slice will be allocated.

versioned_docs/version-v2.x/api/ctx.md

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,37 @@ app.Get("/", func(c *fiber.Ctx) error {
4949
})
5050
```
5151

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+
5283
Fiber provides similar functions for the other accept headers.
5384

5485
```go
@@ -583,7 +614,7 @@ app.Get("/", func(c *fiber.Ctx) error {
583614
584615
## GetReqHeaders
585616

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.
587618

588619
```go title="Signature"
589620
func (c *Ctx) GetReqHeaders() map[string][]string
@@ -618,7 +649,7 @@ app.Get("/", func(c *fiber.Ctx) error {
618649
619650
## GetRespHeaders
620651

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.
622653

623654
```go title="Signature"
624655
func (c *Ctx) GetRespHeaders() map[string][]string
@@ -766,11 +797,11 @@ app.Get("/", func(c *fiber.Ctx) error {
766797
Converts any **interface** or **string** to JSON using the [encoding/json](https://pkg.go.dev/encoding/json) package.
767798
768799
:::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**.
770801
:::
771802
772803
```go title="Signature"
773-
func (c *Ctx) JSON(data interface{}) error
804+
func (c *Ctx) JSON(data interface{}, ctype ...string) error
774805
```
775806
776807
```go title="Example"
@@ -796,6 +827,22 @@ app.Get("/json", func(c *fiber.Ctx) error {
796827
})
797828
// => Content-Type: application/json
798829
// => "{"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+
// => }"
799846
})
800847
```
801848

0 commit comments

Comments
 (0)