Go net/http package¶
The net/http package is a fundamental component in Go for building Microservices and Web distributed systems^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. It is a built-in library that provides functionalities for both HTTP clients and servers^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
Usage¶
To use the package, it must be imported into the Go source code^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]:
import (
"net/http"
)
Client and Server Capabilities¶
The package allows for the implementation of HTTP clients, which make HTTP calls, and HTTP servers, which receive or serve HTTP requests^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. It is capable of sending HTTP requests as well as defining a server for receiving them, allowing users to run an HTTP server to serve files or data (like an API)^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
Starting a Server¶
An HTTP server can be started using the ListenAndServe function^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. This function starts an HTTP server with a given address and handler^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
http.ListenAndServe(":8080", nil)
When the handler parameter is nil, the DefaultServeMux is used^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. Handlers are typically added to DefaultServeMux using Handle or HandleFunc^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
Handling Requests¶
The HandleFunc function is used to register a function that executes when a specific route pattern is matched^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Handle logic here
})
The handler function must accept two arguments^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]:
1. http.ResponseWriter: Used to construct the HTTP response.
2. *http.Request: Represents the incoming HTTP request, providing access to body, headers, and other request data^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
HTTP Responses¶
To send data back to the client, the Write method of the ResponseWriter is used^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. This method accepts a slice of bytes ([]byte)^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
w.Write([]byte("Hello!"))
You can also set the HTTP status code using the WriteHeader method^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
w.WriteHeader(405)
fmt.Fprintf(w, "Method not Supported!")
Headers¶
Headers can be accessed from the request object r.Header, which is a dictionary^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
for header, value := range r.Header {
fmt.Printf("Key: %v \t Value: %v \n", header, value)
}
Response headers can be added using the Header() method on the writer^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
w.Header().Add("TestHeader", "TestValue")
HTTP methods¶
The package supports standard HTTP methods such as GET and POST^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
- GET: Used to request data from a specified resource^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. Data is typically serialized to JSON (e.g., using
json.Marshal) and written to the response^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. - POST: Used to send data to a server to create or update a resource^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. The request body can be read using
ioutil.ReadAll(r.Body)^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md] and then deserialized (e.g., usingjson.Unmarshal)^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
The request method can be checked via r.Method^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
Sources¶
^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]