Golang Web Request Handling

Glitch
2 min readMar 27, 2023

--

Golang is a powerful programming language that has been gaining popularity in recent years. One of the most common use cases for Golang is for web development. In this blog, we will discuss how to handle web requests in Golang.

The first step to handling web requests in Golang is to create a server. This can be done using the net/http package, which provides the necessary tools for creating an HTTP server. The http.ListenAndServe function is used to start the server and listen for incoming requests.

package main

import (
"fmt"
"net/http"
)

func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
}
In the example above, we create a simple HTTP server that listens on port 8080. We use the http.HandleFunc function to define a handler function for the root path ("/"). The handler function takes two arguments: a http.ResponseWriter and a http.Request. The http.ResponseWriter is used to write the response to the client, and the http.Request contains information about the incoming request.

In the handler function, we simply write "Hello, World!" to the response writer using the fmt.Fprintf function.

Now that we have a basic server set up, let's look at how to handle different types of requests. In the http.Request struct, there is a field called Method which indicates the HTTP method used for the request (e.g. GET, POST, PUT, DELETE). We can use a switch statement to handle different types of requests.

func handler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
handleGet(w, r)
case "POST":
handlePost(w, r)
case "PUT":
handlePut(w, r)
case "DELETE":
handleDelete(w, r)
default:
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
}
}

func handleGet(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is a GET request")
}

func handlePost(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is a POST request")
}

func handlePut(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is a PUT request")
}

func handleDelete(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is a DELETE request")
}
In the example above, we define four different handler functions for the four different HTTP methods. In the main handler function, we use a switch statement to determine which handler function to call based on the HTTP method.

If an invalid request method is used, we return a 405 Method Not Allowed error using the http.Error function.

Finally, let's look at how to handle query parameters in Golang. Query parameters are the key-value pairs that come after the "?" in a URL (e.g. http://example.com/search?q=foo&limit=10). In Golang, we can access query parameters using the http.Request.URL.Query() function.

func handleGet(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
name := query.Get("name")
age := query.Get("age")
fmt.Fprintf(w, "Name: %s\nAge: %s", name, age)
}

--

--

No responses yet