mirror of https://github.com/gofiber/fiber.git
Add Format function
parent
31876e3109
commit
d4a051dc85
|
@ -24,7 +24,7 @@ app.Get("/", func(c *fiber.Ctx) {
|
|||
// => "application/json"
|
||||
|
||||
// Accept: text/*, application/json
|
||||
c.Accepts("image/png"
|
||||
c.Accepts("image/png")
|
||||
c.Accepts("png")
|
||||
// => ""
|
||||
})
|
||||
|
@ -321,8 +321,29 @@ app.Get("/", func(c *fiber.Ctx) {
|
|||
})
|
||||
```
|
||||
|
||||
#### !Format
|
||||
!> Planned for v2.0.0
|
||||
#### Format
|
||||
Performs content-negotiation on the Accept HTTP header. It uses [Accepts](#accepts) to select a proper format. If the header is not specified or there is no proper format, text/plain is used.
|
||||
|
||||
```go
|
||||
// Function signature
|
||||
c.Format(body string)
|
||||
c.Format(body []byte)
|
||||
|
||||
// Example
|
||||
app.Get("/", func(c *fiber.Ctx) {
|
||||
// Accept: text/plain
|
||||
c.Format("Hello, World!")
|
||||
// => Hello, World!
|
||||
|
||||
// Accept: text/html
|
||||
c.Format("Hello, World!")
|
||||
// => <p>Hello, World!</p
|
||||
|
||||
// Accept: application/json
|
||||
c.Format("Hello, World!")
|
||||
// => "Hello, World!"
|
||||
})
|
||||
```
|
||||
|
||||
#### FormFile
|
||||
MultipartForm files can be retrieved by name, the first file from the given key is returned.
|
||||
|
@ -444,7 +465,7 @@ app.Get("/", func(c *fiber.Ctx) {
|
|||
```
|
||||
|
||||
#### Json
|
||||
Converts any interface to json using [Jsoniter](https://github.com/json-iterator/go), this functions also sets the content header to application/json.
|
||||
Converts any interface or string to json using [Jsoniter](https://github.com/json-iterator/go), this function also sets the content header to application/json.
|
||||
```go
|
||||
// Function signature
|
||||
c.Json(v interface{}) error
|
||||
|
@ -463,9 +484,13 @@ app.Get("/json", func(c *fiber.Ctx) {
|
|||
}
|
||||
c.Json(data)
|
||||
// => "{"Name": "Grame", "Age": 20}"
|
||||
|
||||
c.Json("Hello, World!")
|
||||
// => "Hello, World!"
|
||||
})
|
||||
app.Listen(8080)
|
||||
```
|
||||
|
||||
Or with error checking
|
||||
```go
|
||||
app.Get("/json", func(c *fiber.Ctx) {
|
||||
|
|
52
response.go
52
response.go
|
@ -19,13 +19,13 @@ import (
|
|||
|
||||
// Append : https://gofiber.github.io/fiber/#/context?id=append
|
||||
func (ctx *Ctx) Append(field string, values ...string) {
|
||||
newVal := ctx.Get(field)
|
||||
value := ctx.Get(field)
|
||||
if len(values) > 0 {
|
||||
for i := range values {
|
||||
newVal = newVal + ", " + values[i]
|
||||
value = fmt.Sprintf("%s, %s", value, values[i])
|
||||
}
|
||||
}
|
||||
ctx.Set(field, newVal)
|
||||
ctx.Set(field, value)
|
||||
}
|
||||
|
||||
// Attachment : https://gofiber.github.io/fiber/#/context?id=attachment
|
||||
|
@ -41,16 +41,15 @@ func (ctx *Ctx) Attachment(name ...string) {
|
|||
|
||||
// ClearCookie : https://gofiber.github.io/fiber/#/context?id=clearcookie
|
||||
func (ctx *Ctx) ClearCookie(name ...string) {
|
||||
if len(name) == 0 {
|
||||
ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) {
|
||||
fmt.Println(b2s(k), b2s(v))
|
||||
ctx.Fasthttp.Response.Header.DelClientCookie(b2s(k))
|
||||
})
|
||||
} else if len(name) > 0 {
|
||||
if len(name) > 0 {
|
||||
for i := range name {
|
||||
ctx.Fasthttp.Response.Header.DelClientCookie(name[i])
|
||||
}
|
||||
return
|
||||
}
|
||||
ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) {
|
||||
ctx.Fasthttp.Response.Header.DelClientCookie(b2s(k))
|
||||
})
|
||||
}
|
||||
|
||||
// Cookie : https://gofiber.github.io/fiber/#/context?id=cookie
|
||||
|
@ -115,8 +114,28 @@ func (ctx *Ctx) End() {
|
|||
}
|
||||
|
||||
// Format : https://gofiber.github.io/fiber/#/context?id=format
|
||||
func (ctx *Ctx) Format() {
|
||||
|
||||
func (ctx *Ctx) Format(args ...interface{}) {
|
||||
if len(args) == 0 {
|
||||
panic("Missing string or []byte body")
|
||||
}
|
||||
var body string
|
||||
switch b := args[0].(type) {
|
||||
case string:
|
||||
body = b
|
||||
case []byte:
|
||||
body = b2s(b)
|
||||
default:
|
||||
panic("Body must be a string or []byte")
|
||||
}
|
||||
accept := ctx.Accepts("html", "json")
|
||||
switch accept {
|
||||
case "html":
|
||||
ctx.SendString("<p>" + body + "</p>")
|
||||
case "json":
|
||||
ctx.Json(body)
|
||||
default:
|
||||
ctx.SendString(body)
|
||||
}
|
||||
}
|
||||
|
||||
// HeadersSent : https://gofiber.github.io/fiber/#/context?id=headerssent
|
||||
|
@ -126,12 +145,13 @@ func (ctx *Ctx) HeadersSent() {
|
|||
|
||||
// Json : https://gofiber.github.io/fiber/#/context?id=json
|
||||
func (ctx *Ctx) Json(v interface{}) error {
|
||||
raw, err := jsoniter.Marshal(&v)
|
||||
|
||||
raw, err := jsoniter.MarshalToString(&v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.Set("Content-Type", "application/json")
|
||||
ctx.Fasthttp.Response.SetBodyString(b2s(raw))
|
||||
ctx.Fasthttp.Response.SetBodyString(raw)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -205,8 +225,8 @@ func (ctx *Ctx) Render() {
|
|||
func (ctx *Ctx) Send(args ...interface{}) {
|
||||
|
||||
// https://github.com/valyala/fasthttp/blob/master/http.go#L490
|
||||
if len(args) != 1 {
|
||||
panic("To many arguments!")
|
||||
if len(args) == 0 {
|
||||
panic("Missing string or []byte body")
|
||||
}
|
||||
switch body := args[0].(type) {
|
||||
case string:
|
||||
|
@ -291,7 +311,7 @@ func (ctx *Ctx) Vary(field ...string) {
|
|||
// Write : https://gofiber.github.io/fiber/#/context?id=write
|
||||
func (ctx *Ctx) Write(args ...interface{}) {
|
||||
if len(args) == 0 {
|
||||
panic("Missing body")
|
||||
panic("Missing string or []byte body")
|
||||
}
|
||||
switch body := args[0].(type) {
|
||||
case string:
|
||||
|
|
Loading…
Reference in New Issue