easyhttp
easyhttp is a small Go package that provides helper functions to simplify common HTTP request and response handling tasks.
Installation
go get github.com/primekobie/easyhttp
Features
Example
package main
import (
"net/http"
"github.com/primekobie/easyhttp"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
func userHandler(w http.ResponseWriter, r *http.Request) {
user := User{ID: 1, Name: "John Doe"}
headers := http.Header{}
headers.Set("X-Custom-Header", "value")
err := easyhttp.WriteJSON(w, http.StatusOK, user, headers)
if err != nil {
// Handle error
}
}
func updateHandler(w http.ResponseWriter, r *http.Request) {
var payload struct { Status string `json:"status"` }
err := easyhttp.ReadJSON(r, &payload) // Read JSON
if err != nil {
// Handle error, for example:
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Process the payload...
easyhttp.WriteJSON(w, http.StatusOK, map[string]string{"status": "updated"}, nil)
}