Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/foresthoffman/rwb
A Go package for intercepting HTTP(s) response values before sending them to the request client.
https://github.com/foresthoffman/rwb
go golang http
Last synced: about 1 month ago
JSON representation
A Go package for intercepting HTTP(s) response values before sending them to the request client.
- Host: GitHub
- URL: https://github.com/foresthoffman/rwb
- Owner: foresthoffman
- License: mit
- Created: 2021-04-16T00:10:44.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-04-16T00:36:58.000Z (over 3 years ago)
- Last Synced: 2024-06-20T14:29:12.814Z (7 months ago)
- Topics: go, golang, http
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## RWB - Response Writer Buffer
The rwb package exposes a wrapper for the `http.ResponseWriter`, which allows for intercepting HTTP(s) response bodies and response headers. The `ResponseWriterBuffer` is lightweight and can assist in HTTP(s) server request handling, as well as server response middleware.
Here's a guide to thinking in terms of *response* middleware and not just *request* middleware:
[Intercepting RESTful Responses with Middleware](https://dev.to/foresthoffman/intercepting-restful-responses-with-middleware-4b64)
### Installation
Run `go get -u github.com/foresthoffman/rwb`
### Importing
Import this package by including `github.com/foresthoffman/rwb` in your import block.
e.g.
```go
package mainimport(
...
"github.com/foresthoffman/rwb"
)
```### Usage
Basic usage:
```go
package mainimport (
"github.com/foresthoffman/rwb"
"log"
"net/http"
)func main() {
// Hit http://localhost:9001/ in your browser, or cURL/wget it! It's up to you.
http.ListenAndServe(":9001", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// You could update the header directly.
w.Header().Set("Content-Type", "application/json")// New header exists.
log.Println(w.Header().Get("Content-Type"))// Or you could write to the buffer, and flush it when you're done.
writerBuf := rwb.New(w)
writerBuf.Header().Set("potato", "russet")// New header doesn't exist yet. It's in the buffer!
log.Println(w.Header().Get("potato"))_, err := writerBuf.Flush()
if err != nil {
panic(err)
}// New header exists!
log.Println(w.Header().Get("potato"))
}))
}
```_That's all, enjoy!_