Socket
Socket
Sign inDemoInstall

github.com/rickb777/negotiator

Package Overview
Dependencies
16
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/rickb777/negotiator

Package negotiator is a library that handles content negotiation in web applications written in Go. Content negotiation is specified by RFC (http://tools.ietf.org/html/rfc7231) and, less formally, by Ajax (https://en.wikipedia.org/wiki/XMLHttpRequest). A Negotiator contains a list of ResponseProcessor. For each call to Negotiate, one or more offers of possibly-matching data is compared with the headers in the request. The best matching response processor is chosen and given the task of sending the response. For more information visit http://github.com/rickb777/negotiator Accept - from https://tools.ietf.org/html/rfc7231#section-5.3.2: The "Accept" header field can be used by user agents to specify response media types that are acceptable. Accept header fields can be used to indicate that the request is specifically limited to a small set of desired types, as in the case of a request for an in-line image. A request without any Accept header field implies that the user agent will accept any media type in response. If the header field is present in a request and none of the available representations for the response have a media type that is listed as acceptable, the origin server can either honor the header field by sending a 406 (Not Acceptable) response, or disregard the header field by treating the response as if it is not subject to content negotiation. Accept-Language - from https://tools.ietf.org/html/rfc7231#section-5.3.5: The "Accept-Language" header field can be used by user agents to indicate the set of natural languages that are preferred in the response. A request without any Accept-Language header field implies that the user agent will accept any language in response. If the header field is present in a request and none of the available representations for the response have a matching language tag, the origin server can either disregard the header field by treating the response as if it is not subject to content negotiation or honor the header field by sending a 406 (Not Acceptable) response. However, the latter is not encouraged, as doing so can prevent users from accessing content that they might be able to use (with translation software, for example).


Version published

Readme

Source

Negotiator

GoDoc Build Status Issues

This is a library that handles content negotiation in HTTP server applications written in Go.

Usage

Simple

To return JSON/XML out of the box simple put this in your route handler:

import "github.com/rickb777/negotiator"
...
func getUser(w http.ResponseWriter, req *http.Request) {
    user := &User{"Joe","Bloggs"}
    n := negotiator.NewWithJSONAndXML()
    n.MustNegotiate(w, req, negotiator.Offer{Data: user})
}

Custom

To add your own negotiator, for example you want to write a PDF with your model, do the following:

  1. Create a type that conforms to the ResponseProcessor interface

  2. Where you call negotiator.New(responseProcessors ...ResponseProcessor), pass in a your custom processor. When your request handler calls negotiator.Negotiate(w, req, offers...) it will render a PDF if your Accept header defined it wanted a PDF response.

When a request is Not Acceptable

Having created a Negotiator with one or more response processors, if a request is handled that is not claimed by and processor, a Not Acceptable (406) response is returned.

By default, this uses the standard http.Error function (from net/http) to render the response, If needed, a custom error handler can be plugged in using Negotiator.WithErrorHandler(myHandler).

Accept Handling

The Accept header is parsed using header.ParseMediaRanges(), which returns the slice of media ranges, e.g.

    // handle Accept-Language
    mediaRanges := header.ParseMediaRanges("application/json;q=0.8, application/xml, application/*;q=0.1")

The resulting slice is sorted according to precedence and quality rules, so in this example the order is {"application/xml", "application/json", "application/*"} because the middle item has an implied quality of 1, whereas the first item has a lower quality.

The other content-negotiation headers, Accept-Language, Accept-Charset, Accept-Encoding, are handled by the header.Parse method, e.g.

    // handle Accept-Language
    acceptLanguages := header.Parse("en-GB,en;q=0.5")

This will contain {"en-GB", "en"} in a header.PrecedenceValues slice, sorted according to precedence rules.

This can be used for Accept-Language, Accept-Charset and Accept-Encoding, as well as Accept. The negotiator in this API uses the Accept header only, though.

Acknowledgement

Many thanks to Jonathan Channon (https://github.com/jchannon) for the original concepts and work on which this was based.

FAQs

Last updated on 19 Dec 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc