Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/elnormous/contenttype

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/elnormous/contenttype

  • v1.0.4
  • Source
  • Go
  • Socket score

Version published
Created
Source

Content-Type support library for Go

This library can be used to parse the value Content-Type header (if one is present) and select an acceptable media type from the Accept header of HTTP request.

Usage

Media types are stored in MediaType structure which has Type (e.g. application), Subtype (e.g. json) and Parameters (e.g. charset: utf-8) attributes. Media types are not stored in a string because media type parameters are part of the media type (RFC 7231, 3.1.1.1. Media Type). To convert a string to MediaType use NewMediaType. To convert MediaType back to string use String function. If the Content-Type header is not present in the request, an empty MediaType is returned.

To get the MediaType corresponding to the incoming request's Content-Type header call GetMediaType and pass the http.Request pointer to it, or to parse any media type string call ParseMediaType. Either function will return error if the value is malformed according to RFC 7231, 3.1.1.5. Content-Type.

To get an acceptable media type from an Accept header of the incoming request call GetAcceptableMediaType and pass the http.Request pointer to it and an array of all the acceptable media types. The function will return the best match following the negotiation rules written in RFC 7231, 5.3.2. Accept or an error if the header is malformed or the content type in the Accept header is not supported. If the Accept header is not present in the request, the first media type from the acceptable type list is returned. If you have an Accept header value string from a source other than req.Header.Values("Accept") you can parse that in the same way using GetAcceptableMediaTypeFromHeader.

import (
	"log"
	"github.com/elnormous/contenttype"
)

func handleRequest(responseWriter http.ResponseWriter, request *http.Request) {
    mediaType, mediaTypeError := contenttype.GetMediaType(request)
    if mediaTypeError != nil {
        // handle the error
    }
    log.Println("Media type:", mediaType.String())

    availableMediaTypes := []MediaType{
        contenttype.NewMediaType("application/json"),
        contenttype.NewMediaType("application/xml"),
    }

    accepted, extParameters, acceptError := contenttype.GetAcceptableMediaType(request, availableMediaTypes)
    if acceptError != nil {
        // handle the error
    }
    log.Println("Accepted media type:", accepted.String(), "extension parameters:", extParameters)
}

FAQs

Package last updated on 28 Feb 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc