This is an attemp to create bite-sized articles to motivate me adding content to this blog regularly. Due to my activity in several technical working groups, communities, standardization bodies, my open source work, and my commitments to my family and my kids, I find it challenging to create long-form content.
These will be an attempt to create smaller pieces of nuggets, that will be topic-specific and may be helpful, and help someone nonethless. So, without further ado, here we go…
You can use the io to get the request body from an HTTP request in io.ReadAll()
function to read the r.Body
object representing the incoming request body. Here is an example of how you can do this:
package main
import (
"errors"
"fmt"
"io"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
// Read the request body
body, err := io.ReadAll(r.Body)
if err != nil {
// Handle the error by sending an HTTP status code
// and message back to the client.
http.Error(w, "Error reading request body",
http.StatusBadRequest)
return
}
// Ensure that request body is closed.
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Print("Error while closing the body",
err.Error())
}
}(r.Body)
// Use the request body here
// For example, echoing the request body back to the client
_, err = w.Write(body)
if err != nil {
fmt.Println("Error while writing the body", err.Error())
return
} // Note: In a real application, check the error from Write
}
func main() {
// Register the handler function for the root path
http.HandleFunc("/", handler)
// Start the HTTP server on port 8080
// ListenAndServe always returns a non-nil error.
// After Shutdown or Close, the returned error is
// ErrServerClosed.
if err := http.ListenAndServe(":8080", nil); err != nil {
if errors.Is(err, http.ErrServerClosed) {
fmt.Println("Server closed.")
return
}
// Unexpected error. Log and exit.
panic(err)
}
}
That was your nugget 😄.
Until the next one… May the source be with you 🦄.