https://github.com/go-http-utils/negotiator
:postbox:An HTTP content negotiator for Go
https://github.com/go-http-utils/negotiator
Last synced: 5 months ago
JSON representation
:postbox:An HTTP content negotiator for Go
- Host: GitHub
- URL: https://github.com/go-http-utils/negotiator
- Owner: go-http-utils
- License: mit
- Created: 2016-11-07T09:28:11.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-05T15:41:39.000Z (almost 9 years ago)
- Last Synced: 2024-06-19T03:05:39.630Z (about 2 years ago)
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# negotiator
[](https://travis-ci.org/go-http-utils/negotiator)
[](https://coveralls.io/github/go-http-utils/negotiator?branch=master)
An HTTP content negotiator for Go
## Installation
```sh
go get -u github.com/go-http-utils/negotiator
```
## Documentation
API documentation can be found here: https://godoc.org/github.com/go-http-utils/negotiator
## Usage
```go
import (
"github.com/go-http-utils/negotiator"
)
negotiator := negotiator.New(req.Header)
```
### Type
```go
// Assume that the Accept header is "text/html, application/*;q=0.9, image/jpeg;q=0.8"
negotiator.Type()
// -> "text/html"
negotiator.Type("text/html", "application/json", "image/jpeg")
// -> "text/html"
negotiator.Type("application/json", "image/jpeg", "text/plain")
// -> "application/json"
negotiator.Type("text/plain")
// -> ""
```
### Encoding
```go
// Assume that the Accept-Encoding header is "gzip, compress;q=0.2, identity;q=0.5"
negotiator.Encoding()
// -> "gzip"
negotiator.Encoding("identity", "gzip")
// -> "gzip"
negotiator.Encoding("compress", "identity")
// -> "identity"
```
### Language
```go
// Assume that the Accept-Language header is "en;q=0.8, es, pt"
negotiator.Language()
// -> "es"
negotiator.Language("en", "es", "fr")
// -> "es"
negotiator.Language("es", "pt")
// -> "es"
```
### Charset
```go
// Assume that the Accept-Charset header is "utf-8, iso-8859-1;q=0.8, utf-7;q=0.2"
negotiator.Charset()
// -> "utf-8"
negotiator.Charset("utf-8", "iso-8859-1", "iso-8859-5")
// -> "utf-8"
negotiator.Charset("iso-8859-5")
// -> ""
```