Skip to content

Commit c1f7ec1

Browse files
Add docs from gofiber/fiber@26cc477
1 parent c4ae55f commit c1f7ec1

File tree

8 files changed

+324
-178
lines changed

8 files changed

+324
-178
lines changed

docs/core/api/bind.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Make copies or use the [**`Immutable`**](./ctx.md) setting instead. [Read more..
2020
- [JSON](#json)
2121
- [MultipartForm](#multipartform)
2222
- [XML](#xml)
23+
- [CBOR](#cbor)
2324
- [Cookie](#cookie)
2425
- [Header](#header)
2526
- [Query](#query)
@@ -226,6 +227,43 @@ Run tests with the following `curl` command:
226227
curl -X POST -H "Content-Type: application/xml" --data "<login><name>john</name><pass>doe</pass></login>" localhost:3000
227228
```
228229

230+
### CBOR
231+
232+
Binds the request CBOR body to a struct.
233+
234+
It is important to specify the correct struct tag based on the content type to be parsed. For example, if you want to parse a CBOR body with a field called `Pass`, you would use a struct field with `cbor:"pass"`.
235+
236+
```go title="Signature"
237+
func (b *Bind) CBOR(out any) error
238+
```
239+
240+
```go title="Example"
241+
// Field names should start with an uppercase letter
242+
type Person struct {
243+
Name string `cbor:"name"`
244+
Pass string `cbor:"pass"`
245+
}
246+
247+
app.Post("/", func(c fiber.Ctx) error {
248+
p := new(Person)
249+
250+
if err := c.Bind().CBOR(p); err != nil {
251+
return err
252+
}
253+
254+
log.Println(p.Name) // john
255+
log.Println(p.Pass) // doe
256+
257+
// ...
258+
})
259+
```
260+
261+
Run tests with the following `curl` command:
262+
263+
```bash
264+
curl -X POST -H "Content-Type: application/cbor" --data "\xa2dnamedjohndpasscdoe" localhost:3000
265+
```
266+
229267
### Cookie
230268

231269
This method is similar to [Body Binding](#body), but for cookie parameters.

docs/core/api/constants.md

Lines changed: 144 additions & 140 deletions
Large diffs are not rendered by default.

docs/core/api/ctx.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,54 @@ app.Get("/", func(c fiber.Ctx) error {
924924
})
925925
```
926926

927+
## CBOR
928+
929+
CBOR converts any interface or string to CBOR encoded bytes.
930+
931+
:::info
932+
CBOR also sets the content header to the `ctype` parameter. If no `ctype` is passed in, the header is set to `application/cbor`.
933+
:::
934+
935+
```go title="Signature"
936+
func (c fiber.Ctx) CBOR(data any, ctype ...string) error
937+
```
938+
939+
```go title="Example"
940+
type SomeStruct struct {
941+
Name string `cbor:"name"`
942+
Age uint8 `cbor:"age"`
943+
}
944+
945+
app.Get("/cbor", func(c fiber.Ctx) error {
946+
// Create data struct:
947+
data := SomeStruct{
948+
Name: "Grame",
949+
Age: 20,
950+
}
951+
952+
return c.CBOR(data)
953+
// => Content-Type: application/cbor
954+
// => \xa2dnameeGramecage\x14
955+
956+
return c.CBOR(fiber.Map{
957+
"name": "Grame",
958+
"age": 20,
959+
})
960+
// => Content-Type: application/cbor
961+
// => \xa2dnameeGramecage\x14
962+
963+
return c.CBOR(fiber.Map{
964+
"type": "https://example.com/probs/out-of-credit",
965+
"title": "You do not have enough credit.",
966+
"status": 403,
967+
"detail": "Your current balance is 30, but that costs 50.",
968+
"instance": "/account/12345/msgs/abc",
969+
})
970+
// => Content-Type: application/cbor
971+
// => \xa5dtypex'https://example.com/probs/out-of-creditetitlex\x1eYou do not have enough credit.fstatus\x19\x01\x93fdetailx.Your current balance is 30, but that costs 50.hinstancew/account/12345/msgs/abc
972+
})
973+
```
974+
927975
## Links
928976

929977
Joins the links followed by the property to populate the response’s [Link](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link) HTTP header field.

docs/core/api/fiber.md

Lines changed: 40 additions & 38 deletions
Large diffs are not rendered by default.

docs/core/client/request.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,15 @@ SetXML method sets XML body in request.
657657
func (r *Request) SetXML(v any) *Request
658658
```
659659

660+
## SetCBOR
661+
662+
SetCBOR method sets the request body using [CBOR](https://cbor.io/) encoding format.
663+
It automatically sets the Content-Type header to `"application/cbor"`.
664+
665+
```go title="Signature"
666+
func (r *Request) SetCBOR(v any) *Request
667+
```
668+
660669
## SetRawBody
661670

662671
SetRawBody method sets body with raw data in request.

docs/core/client/response.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ XML method will unmarshal body to xml.
187187
func (r *Response) XML(v any) error
188188
```
189189

190+
## CBOR
191+
192+
CBOR method will unmarshal body to CBOR.
193+
194+
```go title="Signature"
195+
func (r *Response) CBOR(v any) error
196+
```
197+
190198
## Save
191199

192200
Save method will save the body to a file or io.Writer.

docs/core/client/rest.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ type Client struct {
8181
jsonUnmarshal utils.JSONUnmarshal
8282
xmlMarshal utils.XMLMarshal
8383
xmlUnmarshal utils.XMLUnmarshal
84+
cborMarshal utils.CBORMarshal
85+
cborUnmarshal utils.CBORUnmarshal
8486

8587
cookieJar *CookieJar
8688

@@ -314,6 +316,40 @@ SetXMLUnmarshal sets the XML decoder.
314316
func (c *Client) SetXMLUnmarshal(f utils.XMLUnmarshal) *Client
315317
```
316318

319+
### CBOR
320+
321+
#### CBORMarshal
322+
323+
CBORMarshal returns CBOR marshal function in Core.
324+
325+
```go title="Signature"
326+
func (c *Client) CBORMarshal() utils.CBORMarshal
327+
```
328+
329+
#### CBORUnmarshal
330+
331+
CBORUnmarshal returns CBOR unmarshal function in Core.
332+
333+
```go title="Signature"
334+
func (c *Client) CBORUnmarshal() utils.CBORUnmarshal
335+
```
336+
337+
#### SetCBORMarshal
338+
339+
SetCBORMarshal sets CBOR encoder.
340+
341+
```go title="Signature"
342+
func (c *Client) SetCBORMarshal(f utils.CBORMarshal) *Client
343+
```
344+
345+
#### SetCBORUnmarshal
346+
347+
SetCBORUnmarshal sets CBOR decoder.
348+
349+
```go title="Signature"
350+
func (c *Client) SetCBORUnmarshal(f utils.CBORUnmarshal) *Client
351+
```
352+
317353
### TLS
318354

319355
#### TLSConfig

docs/core/whats_new.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ testConfig := fiber.TestConfig{
310310
- **SendString**: Similar to Express.js, sends a string as the response.
311311
- **String**: Similar to Express.js, converts a value to a string.
312312
- **ViewBind**: Binds data to a view, replacing the old `Bind` method.
313+
- **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.
313314

314315
### Removed Methods
315316

0 commit comments

Comments
 (0)