https://github.com/clavoie/httpu
Http handler utilities for Go
https://github.com/clavoie/httpu
go golang http-handler utilities
Last synced: about 1 month ago
JSON representation
Http handler utilities for Go
- Host: GitHub
- URL: https://github.com/clavoie/httpu
- Owner: clavoie
- License: mit
- Created: 2018-08-22T12:50:33.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-21T18:15:06.000Z (about 5 years ago)
- Last Synced: 2025-04-12T00:49:42.701Z (about 1 month ago)
- Topics: go, golang, http-handler, utilities
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# httpu [](http://godoc.org/github.com/clavoie/httpu) [](https://travis-ci.org/clavoie/httpu) [](https://codecov.io/gh/clavoie/httpu) [](https://goreportcard.com/report/github.com/clavoie/httpu)
Http handler utilities for Go.
httpu provides several convenience functions to remove some of the boilerplate from top level http handlers:
```go
func Handler(w http.ResponseWriter, r *http.Request) {
request := new(MyRequest)
if httpu.DecodeJsonOr400(w, r, request, "Could not decode request") {
// http.StatusBadRequest has been written to the response, we can now exit the handler
return
}
result, err := DoWork(request)
if httpu.Write500IfErr(err, w, "Could not process request for: %v", request.Value) {
// http.StatusInternalServerError has been written to the response
return
}
httpu.EncodeJsonOr400(w, result, "Could not encode response json for: %v", result.Value)
}
```
A full example is available [here](https://godoc.org/github.com/clavoie/httpu#example-WriteIfErr)## Dependency Injection
httpu provides an interface that wraps all top level functions if you would prefer to inject the package into your project. A full example of using httpu with dependency injection is [here](https://godoc.org/github.com/clavoie/httpu#example-Impl)