Go language contains net/http package to work with http server, without any external package. net/http is useful when you need high level of control on all most everything.
Table of contents
Open Table of contents
HTTP Package
net/http is a powerful go package for handling HTTP clients and server. it provides essential tools for building web servers, RESTful services or any http related utlity.
it contains handler interface which lets you handle the flow of requests, and many interface to handle routing, parsing etc
lets create a basic http server using http.ListenAndServe which takes url and handler as parameter
package main
import (
"net/http"
)
func main() {
http.ListenAndServe(":8000", nil)
}
well server is running but not doing anything, lets print somthing when a request hits server
package main
import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println("Hello!!! we got the request")
})
http.ListenAndServe(":8000", nil)
}
so where we can see we added http.HandleFunc it takes url pattern and a function handler where we can write business logic
to test it, we can use curl curl -v localhost:8000 now check log in terminal
Reading from Request
http.Request is an interface which contains everything related to request like header, data, method etc
Lets try to read data from request’s body, Request.body implement uses io reader, so we need io reader to read data from request
package main
import (
"io"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data, _ := io.ReadAll(r.Body)
log.Printf("%s", data)
})
http.ListenAndServe(":8000", nil)
}
You can explore other components of request interface, one example would be lets say we want query parameters from url
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := r.URL.Query()
log.Printf("%s", data)
})
http.ListenAndServe(":8000", nil)
}
use curl -v -d "test data" localhost:8000/?key1=value1
Writing in Response
Once you get data and processing is completed, you need to send some data to user or acknowledge the request we can achieve that using ResponseWriter, it implements io writer so we can write data using formatter or directly using Write function
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "youre getting served\n")
})
http.ListenAndServe(":8000", nil)
}
lets create a full basic flow of request and response in go way, we need to handle errors, http provides http.Error which is very helpful to send errored out data with appropriate content-type
http package consists of all the response code like http.StatusBadRequest
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Uh oh!!!", http.StatusBadRequest)
return
}
fmt.Fprintf(w, "we got your data \ndata: %s\n", data)
})
http.ListenAndServe(":8000", nil)
}
thats all about basic operations for http server, we will explore more…