Code Monkey home page Code Monkey logo

negotiator's Introduction

negotiator

Build Status codecov Go Report Card GoDoc License

An HTTP content negotiator for Go

Thanks to negotiator which is the original version written in javascript.

Installation

To install negotiator, you need to install Go and set your Go workspace first.

The first need Go installed (version 1.11+ is required), then you can use the below Go command to install negotiator.

$ go get -u github.com/soongo/negotiator

Usage

import "github.com/soongo/negotiator"

// The negotiator constructor receives a http.Header object
negotiator := negotiator.New(header)

Accept Negotiation

availableMediaTypes := []string{"text/html", "text/plain", "application/json"}

// Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg;q=0.8'

negotiator.MediaTypes()
// -> ["text/html", "image/jpeg", "application/*"]

negotiator.MediaTypes(availableMediaTypes)
// -> ["text/html", "application/json"]

negotiator.MediaType(availableMediaTypes)
// -> "text/html"

Methods

MediaType()

Returns the most preferred media type from the client.

MediaType(availableMediaType)

Returns the most preferred media type from a list of available media types.

MediaTypes()

Returns an array of preferred media types ordered by the client preference.

MediaTypes(availableMediaTypes)

Returns an array of preferred media types ordered by priority from a list of available media types.

Accept-Language Negotiation

availableLanguages := []string{"en", "es", "fr"}

// Let's say Accept-Language header is 'en;q=0.8, es, pt'

negotiator.Languages()
// -> ["es", "pt", "en"]

negotiator.Languages(availableLanguages)
// -> ["es", "en"]

negotiator.Language(availableLanguages)
// -> "es"

Methods

Language()

Returns the most preferred language from the client.

Language(availableLanguages)

Returns the most preferred language from a list of available languages.

Languages()

Returns an array of preferred languages ordered by the client preference.

Languages(availableLanguages)

Returns an array of preferred languages ordered by priority from a list of available languages.

Accept-Charset Negotiation

availableCharsets := []string{"utf-8", "iso-8859-1", "iso-8859-5"}

// Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'

negotiator.Charsets()
// -> ["utf-8", "iso-8859-1", "utf-7"]

negotiator.Charsets(availableCharsets...)
// -> ["utf-8", "iso-8859-1"]

negotiator.Charset(availableCharsets...)
// -> "utf-8"

Methods

Charset()

Returns the most preferred charset from the client.

Charset(availableCharsets...)

Returns the most preferred charset from a list of available charsets.

Charsets()

Returns an array of preferred charsets ordered by the client preference.

Charsets(availableCharsets...)

Returns an array of preferred charsets ordered by priority from a list of available charsets.

Accept-Encoding Negotiation

availableEncodings := []string{"identity", "gzip"}

// Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'

negotiator.Encodings()
// -> ["gzip", "identity", "compress"]

negotiator.Encodings(availableEncodings...)
// -> ["gzip", "identity"]

negotiator.Encoding(availableEncodings...)
// -> "gzip"

Methods

Encoding()

Returns the most preferred encoding from the client.

Encoding(availableEncodings...)

Returns the most preferred encoding from a list of available encodings.

Encodings()

Returns an array of preferred encodings ordered by the client preference.

Encodings(availableEncodings...)

Returns an array of preferred encodings ordered by priority from a list of available encodings.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.