https://github.com/joegasewicz/status-writer
Utility pkg that helps get the status code from the response writer
https://github.com/joegasewicz/status-writer
Last synced: 6 months ago
JSON representation
Utility pkg that helps get the status code from the response writer
- Host: GitHub
- URL: https://github.com/joegasewicz/status-writer
- Owner: joegasewicz
- License: mit
- Created: 2022-09-17T20:40:27.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-09-17T21:31:03.000Z (about 3 years ago)
- Last Synced: 2025-04-10T13:34:13.804Z (6 months ago)
- Language: Go
- Homepage: https://pkg.go.dev/github.com/joegasewicz/status-writer
- Size: 4.88 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Status Writer
Utility pkg that helps get the status code from the response writer## Install
```
go get github.com/joegasewicz/status-writer
```## Setup
Example shows setup for a middleware function. This could be a logging
function or similar whereby you require access to the status code from
`http.ResponseWriter`
```go
func MyMiddleware(h http.HandleFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sw := status_writer.New(w) // Here we override http.ResponseWriter's `WriteHeader` function
h(sw, r)
statusCode := sw.Status // Now work with the status code
}
}
```### Usage
Now you can set the status code as usual via the response writer in your
handlers
```go
func (n *Notice) Get(w http.ResponseWriter, request *http.Request) {
w.WriteHeader(http.StatusAccepted)
}
```