JSON encodes data as JSON and writes it to the response.
(data interface{}, status ...int)
| 617 | |
| 618 | // JSON encodes data as JSON and writes it to the response. |
| 619 | func (c *Ctx) JSON(data interface{}, status ...int) error { |
| 620 | raw, err := c.Server.config.JSONEncoder(data) |
| 621 | if err != nil { |
| 622 | return err |
| 623 | } |
| 624 | c.Response.Header().Set("Content-Type", "application/json") |
| 625 | if len(status) > 0 { |
| 626 | c.Response.WriteHeader(status[0]) |
| 627 | } else { |
| 628 | c.Response.WriteHeader(http.StatusOK) |
| 629 | } |
| 630 | c.Response.Write(raw) |
| 631 | return nil |
| 632 | } |
| 633 | |
| 634 | // Status sets the HTTP response status code. |
| 635 | func (c *Ctx) Status(status int) *Ctx { |