Rendering

As mentioned previously, the job of a web application or an API is to create an HTTP response message, given an HTTP request message. Echo has a really nice facility to help us render responses from our handlers and middleware functions. echo.Context has methods which perform all of the heavy hitting for rendering responses back to the caller. As seen in prior examples, there is a JSON method on the Echo Context which allows for rendering of a JSON payload back to the caller. The following is a list of supported rendering capabilities within Echo:

  • HTML: Render an HTML response:

    • HTMLBlob: Render a preformed HTML response

  • JSON: Render a JSON response, converting the variable into JSON based on the struct tags of the variable:

    • JSONBlob: Render a preformed JSON response

    • JSONPretty: Render a pretty JSON response

    • JSONP: Render a JSONP response based on the struct tags of the variable

  • XML: Render an XML response based on the struct tags of the variable:

    • XMLBlob: Render a preformed XML string

    • XMLPretty: Render a pretty XML document

  • File: Render a file response:

  • Attachment: Render an attachment response

  • Blob: Render a byte blob as the response

  • String: Render a string as the response

  • NoContent: Do not render a response body

  • Redirect: Render a redirect response pointing the result to another location

All of the preceding methods provided in Echo allow you flexibility in rendering your application. Within a typical API, you will likely use a structured response rendering mechanism such as JSON or XML. If you are working on a server-side rendered web application, the HTML response mechanisms with built-in template rendering may make more sense for your use case.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.120.136