GoLang’s standard library provides robust support for HTTP, making it an excellent choice for web development. The net/http
package in GoLang allows you to build web servers, create HTTP clients, and work with common web protocols and formats. This lesson will cover how to build HTTP clients and servers, handling requests and responses, and developing web applications in GoLang.
1. Building an HTTP Server
An HTTP server in GoLang is simple to set up using the net/http
package. The server listens for HTTP requests on a specific port and responds with the appropriate content.
Basic HTTP Server:
package main
import (
"fmt"
"net/http"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
}
func main() {
http.HandleFunc("/", helloHandler)
fmt.Println("Starting server on :8080")
http.ListenAndServe(":8080", nil)
}
/
route returns “Hello, World!”/about
route returns “About Page.”
2. Handling HTTP Requests
You can extract various details from the incoming HTTP requests, such as headers, query parameters, and request bodies.
Extracting Query Parameters:
func queryHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
name := query.Get("name")
fmt.Fprintf(w, "Hello, %s!", name)
}
func main() {
http.HandleFunc("/query", queryHandler)
http.ListenAndServe(":8080", nil)
}
If the URL is /query?name=GoLang
, the response will be “Hello, GoLang!”
Reading HTTP Headers:
func headerHandler(w http.ResponseWriter, r *http.Request) {
userAgent := r.Header.Get("User-Agent")
fmt.Fprintf(w, "User-Agent: %s", userAgent)
}
func main() {
http.HandleFunc("/headers", headerHandler)
http.ListenAndServe(":8080", nil)
}
- This example prints the
User-Agent
header from the request.
Processing Request Body (POST Requests):
func postHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
body := r.Body
defer body.Close()
bodyData, _ := io.ReadAll(body)
fmt.Fprintf(w, "Received: %s", string(bodyData))
} else {
fmt.Fprintf(w, "Invalid request method.")
}
}
func main() {
http.HandleFunc("/submit", postHandler)
http.ListenAndServe(":8080", nil)
}
This code handles POST requests, reads the request body, and returns it to the client.
3. Creating an HTTP Client
You can use GoLang’s http.Client
to make HTTP requests to other web servers. This is useful when you need to interact with third-party APIs or services.
Making a GET Request:
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
resp, err := http.Get("https://api.github.com")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
Explanation:
http.Get()
: Sends a GET request to the specified URL.ioutil.ReadAll()
: Reads the response body into a byte slice, which is then printed.
Making a POST Request:
package main
import (
"bytes"
"fmt"
"net/http"
)
func main() {
jsonData := []byte(`{"name": "GoLang"}`)
resp, err := http.Post("https://jsonplaceholder.typicode.com/posts", "application/json", bytes.NewBuffer(jsonData))
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("Status:", resp.Status)
}
- This code makes a POST request with JSON data to a REST API.
4. Working with Middleware
In GoLang, you can create middleware functions to intercept and process HTTP requests before they reach their final destination. This is useful for adding logging, authentication, or other pre-processing steps.
Example Middleware:
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Request URL: %s\n", r.URL.Path)
next.ServeHTTP(w, r)
})
}
func main() {
helloHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
})
http.Handle("/", loggingMiddleware(helloHandler))
http.ListenAndServe(":8080", nil)
}
- The middleware logs the request URL before passing control to the
helloHandler
.
5. Serving Static Files
GoLang’s http.FileServer
is used to serve static assets like HTML, CSS, and JavaScript files.
Serving Static Files:
func main() {
fileServer := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fileServer))
http.ListenAndServe(":8080", nil)
}
- The files from the
./static
directory are served when a request is made to/static/
.
Key Takeaways:
- HTTP Server: GoLang makes it simple to create powerful web servers using the
net/http
package. You can route requests, handle different HTTP methods, and serve static files. - HTTP Client: The
http.Client
allows you to make GET, POST, and other types of HTTP requests to interact with external APIs. - Middleware: Middleware is used to preprocess HTTP requests, which is especially useful for logging or security tasks.
- Static File Serving: The
http.FileServer
allows you to serve static files like HTML, CSS, and JavaScript.
These tools make GoLang an efficient and capable language for building web applications, APIs, and handling HTTP communication.