Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/qor/responder
Responder: Respond differently according to request's accepted mime type
https://github.com/qor/responder
Last synced: 5 days ago
JSON representation
Responder: Respond differently according to request's accepted mime type
- Host: GitHub
- URL: https://github.com/qor/responder
- Owner: qor
- License: mit
- Created: 2015-12-11T02:03:30.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-10-15T10:47:56.000Z (about 4 years ago)
- Last Synced: 2024-06-18T20:09:39.603Z (5 months ago)
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 6
- Watchers: 19
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Responder
Responder provides a means to respond differently according to a request's accepted mime type.
[![GoDoc](https://godoc.org/github.com/qor/responder?status.svg)](https://godoc.org/github.com/qor/responder)
[![Build Status](https://travis-ci.com/qor/responder.svg?branch=master)](https://travis-ci.com/qor/responder)## Usage
### Register mime type
```go
import "github.com/qor/responder"responder.Register("text/html", "html")
responder.Register("application/json", "json")
responder.Register("application/xml", "xml")
```[Responder](https://github.com/qor/responder) has the above 3 mime types registered by default. You can register more types with the `Register` function, which accepts 2 parameters:
1. The mime type, like `text/html`
2. The format of the mime type, like `html`### Respond to registered mime types
```go
func handler(writer http.ResponseWriter, request *http.Request) {
responder.With("html", func() {
writer.Write([]byte("this is a html request"))
}).With([]string{"json", "xml"}, func() {
writer.Write([]byte("this is a json or xml request"))
}).Respond(request)
})
```The first `html` in the example will be the default response type if [Responder](https://github.com/qor/responder) cannot find a corresponding mime type.
## License
Released under the [MIT License](http://opensource.org/licenses/MIT).