## HTTP Negotiation ## What do this library? This library provides a simple way to negotiate the content type of HTTP request. It supports: * [x] [Content Negotiation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation) * [x] [Accept-Language](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language) * [x] [Accept-Charset](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Charset) * [x] [Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) ## Installation ```shell go get gitlab.schukai.com/oss/libraries/go/network/http-negotiation ``` **Note:** This library uses [Go Modules](https://github.com/golang/go/wiki/Modules) to manage dependencies. ## Usage ### Content Negotiation With the `Type` function you can negotiate the content type of HTTP request. ```go package main import ( "net/http" "gitlab.schukai.com/oss/libraries/go/network/http-negotiation" ) func handleRequest(w http.ResponseWriter, r *http.Request) { n := negotiation.New(r.Header) if n.Type("application/json") == "application/json" { w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"message": "Hello World!"}`)) return } } ``` ### Accept Content Type With the `ContentType` function you can negotiate the content type of HTTP request. ```go package main import ( "net/http" "gitlab.schukai.com/oss/libraries/go/network/http-negotiation" ) func handleRequest(w http.ResponseWriter, r *http.Request) { n := negotiation.New(r.Header) if n.ContentType("application/json") != "" { // ... } } ``` ### Language Negotiation With the `Language` function you can negotiate the language of HTTP request. ```go package main import ( "net/http" "gitlab.schukai.com/oss/libraries/go/network/http-negotiation" ) func handleRequest(w http.ResponseWriter, r *http.Request) { n := negotiation.New(r.Header) if n.Language("en-GB", "en") != "en" { // ... } } ``` ### Charset Negotiation With the `Charset` function you can negotiate the charset of HTTP request. ```go package main import ( "net/http" "gitlab.schukai.com/oss/libraries/go/network/http-negotiation" ) func handleRequest(w http.ResponseWriter, r *http.Request) { n := negotiation.New(r.Header) if n.Charset("utf-8", "iso-8859-1") != "utf-8" { // ... } } ``` ### Encoding Negotiation With the `Encoding` function you can negotiate the encoding of HTTP request. ```go package main import ( "net/http" "gitlab.schukai.com/oss/libraries/go/network/http-negotiation" ) func handleRequest(w http.ResponseWriter, r *http.Request) { n := negotiation.New(r.Header) if n.Encoding("gzip", "deflate") != "gzip" { // ... } } ``` ## Contributing Merge requests are welcome. For major changes, please open an issue first to discuss what you would like to change. **Please make sure to update tests as appropriate.** Versioning is done with [SemVer](https://semver.org/). Changelog is generated with [git-chglog](https://github.com/git-chglog/git-chglog#git-chglog) Commit messages should follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) specification. Messages are started with a type, which is one of the following: - **feat**: A new feature - **fix**: A bug fix - **doc**: Documentation only changes - **refactor**: A code change that neither fixes a bug nor adds a feature - **perf**: A code change that improves performance - **test**: Adding missing or correcting existing tests - **chore**: Other changes that don't modify src or test files The footer would be used for a reference to an issue or a breaking change. A commit that has a footer `BREAKING CHANGE:`, or appends a ! after the type/scope, introduces a breaking API change (correlating with MAJOR in semantic versioning). A BREAKING CHANGE can be part of commits of any type. the following is an example of a commit message: ```text feat: add 'extras' field ``` ## License [AGPL-3.0](https://choosealicense.com/licenses/agpl-3.0/)