Documentation ¶
Index ¶
Examples ¶
Constants ¶
View Source
const Version = "1.0.0"
Version is this package's version
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Negotiator ¶
type Negotiator struct {
// contains filtered or unexported fields
}
Negotiator repensents the HTTP negotiator.
func (*Negotiator) Charset ¶
func (n *Negotiator) Charset(offers ...string) (bestOffer string)
Charset returns the most preferred charset from the HTTP Accept-Charset header. If nothing accepted, then empty string is returned.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/go-http-utils/negotiator" ) func main() { req := httptest.NewRequest(http.MethodGet, "/", nil) req.Header.Set("Accept-Language", "utf-8, iso-8859-1;q=0.8, utf-7;q=0.2") negotiator := negotiator.New(req.Header) fmt.Println(negotiator.Charset("UTF-8", "ISO-8859-1", "ISO-8859-5")) // -> "UTF-8" fmt.Println(negotiator.Charset("ISO-8859-5")) // -> "" }
Output:
func (*Negotiator) Encoding ¶
func (n *Negotiator) Encoding(offers ...string) (bestOffer string)
Encoding returns the most preferred encoding from the HTTP Accept-Encoding header. If nothing accepted, then empty string is returned.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/go-http-utils/negotiator" ) func main() { req := httptest.NewRequest(http.MethodGet, "/", nil) req.Header.Set("Accept-Encoding", "gzip, compress;q=0.2, identity;q=0.5") negotiator := negotiator.New(req.Header) fmt.Println(negotiator.Encoding("identity", "gzip")) // -> "gzip" fmt.Println(negotiator.Encoding("compress", "identity")) // -> "identity" }
Output:
func (*Negotiator) Language ¶
func (n *Negotiator) Language(offers ...string) (bestOffer string)
Language returns the most preferred language from the HTTP Accept-Language header. If nothing accepted, then empty string is returned.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/go-http-utils/negotiator" ) func main() { req := httptest.NewRequest(http.MethodGet, "/", nil) req.Header.Set("Accept-Language", "en;q=0.8, es, pt") negotiator := negotiator.New(req.Header) fmt.Println(negotiator.Language("en", "es", "fr")) // -> "es" fmt.Println(negotiator.Language("es", "pt")) // -> "es" }
Output:
func (*Negotiator) Type ¶
func (n *Negotiator) Type(offers ...string) (bestOffer string)
Type returns the most preferred content type from the HTTP Accept header. If nothing accepted, then empty string is returned.
Click to show internal directories.
Click to hide internal directories.