snet is a simple Go library that provides helper functions for the net/http package. It simplifies the process of creating HTTP requests, (un)marshalling, constructing URLs, and gracefully shutting down servers.
To install snet, use the following command:
go get github.com/mrbanja/snet/v2Here's a brief overview of the functions provided by snet:
NewRequest creates a new HTTP request with the given method, URL, and body. The body is an optional JSON-serializable struct.
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
var body = &User{Name: "John", Age: 30}
req, err := snet.NewRequest(context.TODO(), "POST", "https://example.com", body)Unmarshal unmarshals the response body into a new instance of the provided type.
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
user, err := sreq.UnmarshalResp[User](resp)U creates a new url.URL instance with the given path appended to the base URL.
u, err := snet.U("https://example.com/api", "/user/create")ListenAndServe starts the server and listens for signals to shut down the server.
err := snet.ListenAndServe(ctx, server, logger, os.Interrupt)snet also provides custom errors. You can:
- Create a new error using
NewWrongStatusError(...) - Check if an error is of a specific type using
IsWrongStatusError(...)
This error type includes the response body, response status code, request URL, and request method.
if req.StatusCode != http.StatusOK {
return snet.NewWrongStatusError(req)
}The output will be:
wrong status code for [GET https://example.com]; Code: [405] with reponse [Method Not Allowed, Use POST instead]
Contributions are welcome! Please feel free to submit a Pull Request.
snet is released under the MIT License.