Handling Requests in Echo!

a. Routing and URL parameters

b. Query parameters and form data

c. Middleware and HTTP context

Echo is a popular web framework for building web applications in Go. In the previous article, we introduced Echo and learned how to set up a basic project structure. In this article, we will focus on handling requests in Echo.

Routing and URL Parameters

Routing is the process of mapping HTTP requests to the appropriate handler function based on the URL path and HTTP method. In Echo, routing is done using the Echo object's Router method. Here's an example:

package main

import (
    "net/http"

    "github.com/labstack/echo"
)

func main() {
    e := echo.New()

    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })

    e.GET("/users/:id", func(c echo.Context) error {
        id := c.Param("id")
        return c.String(http.StatusOK, "User ID: "+id)
    })

    e.Start(":8080")
}

In this example, we defined two routes: / and /users/:id. The first route maps to a handler function that returns a simple string. The second route maps to a handler function that extracts the id parameter from the URL using the Param method of the echo.Context object.

Query Parameters and Form Data

Query parameters and form data are commonly used in web applications to send data from the client to the server. In Echo, query parameters and form data can be accessed using the QueryParam and FormValue methods of the echo.Context object, respectively.

Here's an example:

package main

import (
    "net/http"

    "github.com/labstack/echo"
)

func main() {
    e := echo.New()

    e.GET("/books", func(c echo.Context) error {
        author := c.QueryParam("author")
        title := c.FormValue("title")
        return c.String(http.StatusOK, "Author: "+author+", Title: "+title)
    })

    e.Start(":8080")
}

In this example, we defined a route /books that expects an author query parameter and a title form value. The QueryParam and FormValue methods are used to extract these values from the request.

Middleware and HTTP Context

Middleware is a powerful feature in Echo that allows you to modify the HTTP request and response objects before and after they are processed by the handler functions. Middleware functions are defined using the echo.MiddlewareFunc type, and can be added to the request chain using the Use method of the Echo object.

Here's an example:

package main

import (
    "net/http"

    "github.com/labstack/echo"
    "github.com/labstack/echo/middleware"
)

func main() {
    e := echo.New()

    e.Use(middleware.Logger())

    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })

    e.Start(":8080")
}

In this example, we defined a logger middleware function using the middleware.Logger function provided by Echo. This middleware function is added to the request chain using the Use method of the Echo object. The Logger function will log information about each request, such as the HTTP method, URL path, and response status code.

Conclusion

In this article, we learned how to handle requests in Echo by defining routes, accessing query parameters and form data, and using middleware functions. Echo provides a simple and elegant API for handling HTTP requests, and its middleware support makes it a powerful tool for building web applications in Go.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!