Response
Send String
Use the context method Context#String(code int, s string) to respond with HTTP status code and plain text data.
func(c slim.Context) error { return c.String(http.StatusOK, "Hello, World!")}Send HTML
Use the context method Context#HTML(code int, html string) to send simple HTML text. If we want to send dynamically generated HTML, refer to the Template chapter.
func(c slim.Context) error { return c.HTML(http.StatusOK, "<strong>Hello, World!</strong>")}Send HTML Blob
Use the context method Context#HTMLBlob(code int, b []byte) to send HTML blob content with status.
Render Template
Refer to the Template chapter for more information.
Send JSON
Use the context method Context#JSON(code int, i any) to serialize the provided Go type data to JSON data and respond with status code.
// Usertype User struct { Name string `json:"name" xml:"name"` Email string `json:"email" xml:"email"`}// Handlerfunc(c slim.Context) error { u := &User{ Name: "Jon", Email: "jon@go-slim.dev", } return c.JSON(http.StatusOK, u)}Stream JSON
Since the method Context#JSON() internally uses json.Marshal to serialize data, it may not be efficient for large JSON. In this case, we can use streaming to transmit JSON, example:
func(c slim.Context) error { u := &User{ Name: "Jon", Email: "jon@go-slim.dev", } c.Response().Header().Set(slim.HeaderContentType, slim.MIMEApplicationJSONCharsetUTF8) c.Response().WriteHeader(http.StatusOK) return json.NewEncoder(c.Response()).Encode(u)}Pretty JSON
Use the context method Context#JSONPretty(code int, i any, indent string) to send JSON data indented with spaces or tabs.
The example below sends JSON data indented with spaces:
func(c slim.Context) error { u := &User{ Name: "Jon", Email: "joe@go-slim.dev", } return c.JSONPretty(http.StatusOK, u, " ")}{ "email": "joe@go-slim.dev", "name": "Jon"}JSON Blob
Use the context method Context#JSONBlob(code int, b []byte) to directly send pre-encoded JSON blob from external sources, such as from a database.
func(c slim.Context) error { encodedJSON := []byte{} // Encoded JSON from external source return c.JSONBlob(http.StatusOK, encodedJSON)}Send JSONP
Use the context method Context#JSONP(code int, callback string, i any) to respond with provided JSON data in a JavaScript script callback manner.
Send XML
Use the context method Context#XML(code int, i any) to encode the provided Go type data to XML format text and send it with status code.
func(c slim.Context) error { u := &User{ Name: "Jon", Email: "jon@go-slim.dev", } return c.XML(http.StatusOK, u)}Stream XML
The context method Context#XML uses Go’s built-in xml.Marshal to encode data, which may not be efficient for large XML. In this case, we can speed up response by transmitting XML through streaming.
func(c slim.Context) error { u := &User{ Name: "Jon", Email: "jon@go-slim.dev", } c.Response().Header().Set(slim.HeaderContentType, slim.MIMEApplicationXMLCharsetUTF8) c.Response().WriteHeader(http.StatusOK) return xml.NewEncoder(c.Response()).Encode(u)}Pretty XML
Use the context method Context#XMLPretty(code int, i any, indent string) to send XML data indented with spaces or tabs.
The example below sends XML data indented with spaces:
func(c slim.Context) error { u := &User{ Name: "Jon", Email: "joe@go-slim.dev", } return c.XMLPretty(http.StatusOK, u, " ")}Result:
<?xml version="1.0" encoding="UTF-8"?><User> <Name>Jon</Name> <Email>joe@go-slim.dev</Email></User>XML Blob
Use the context method Context#XMLBlob(code int, b []byte) to directly send pre-encoded XML blob data from external sources, such as from a database.
func(c slim.Context) error { encodedXML := []byte{} // Encoded XML from external source return c.XMLBlob(http.StatusOK, encodedXML)}Send File
Use the context method Context#File(file string) to send file content based on Slim#Filesystem as response. It automatically sets the correct content type and handles caching gracefully.
func(c slim.Context) error { return c.File("<PATH_TO_YOUR_FILE>")}Send Attachment
Use the context method Context#Attachment(file, name string) to send a file as an attachment. The browser will download the file to the user’s device using the name we provide.
func(c slim.Context) error { return c.Attachment("<PATH_TO_YOUR_FILE>", "<ATTACHMENT_NAME>")}Send Inline File
Use the context method Context#Inline(file, name string) to send a file inline (i.e., as a webpage or part of a page) to the browser.
func(c slim.Context) error { return c.Inline("<PATH_TO_YOUR_FILE>")}Send Blob
Use the context method Context#Blob(code int, contentType string, b []byte) for byte slices, but we must provide the correct MIME type so clients can understand our intent.
func(c slim.Context) (err error) { data := []byte(`0306703,0035866,NO_ACTION,06/19/2006 0086003,"0005866",UPDATED,06/19/2006`) return c.Blob(http.StatusOK, "text/csv", data)}Send Stream
Use the context method Context#Stream(code int, contentType string, r io.Reader) to send data from an io.Reader interface as a streaming response. We must provide the correct MIME type so clients can understand our intent.
func(c slim.Context) error { f, err := os.Open("<PATH_TO_IMAGE>") if err != nil { return err } return c.Stream(http.StatusOK, "image/png", f)}Send Nothing
Use the context method Context#NoContent(code int) to send the specified status code. This method does not respond with any content.
The commonly used status code is 204 - http.StatusNoContent.
func(c slim.Context) error { return c.NoContent(http.StatusOK)}Redirect Request
Use the context method Context#Redirect(code int, url string) for request redirection.
// handler a requestfunc(c slim.Context) error { return c.Redirect(http.StatusMovedPermanently, "<URL>")}