Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

vimagination.zapto.org/httpaccept

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vimagination.zapto.org/httpaccept

Go Modules
Version
v1.0.2
Version published
Created
Source

httpaccept

CI Go Reference Go Report Card

-- import "vimagination.zapto.org/httpaccept"

Package httpaccept provides a function to deal with the Accept header.

Highlights

  • Simple handling of Accept HTTP header.
  • Supports wildcards, and q-values.

Usage

package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"

	"vimagination.zapto.org/httpaccept"
)

func handler(w http.ResponseWriter, r *http.Request) {
	if !httpaccept.HandleAccept(r, httpaccept.HandlerFunc(func(m httpaccept.Mime) bool {
		if m.Match("image/png") {
			io.WriteString(w, "png")
		} else if m.Match("image/*") {
			io.WriteString(w, "image")
		} else if m.Match("*/other") {
			io.WriteString(w, "other")
		} else {
			return false
		}

		return true
	})) {
		io.WriteString(w, "none")
	}
}

func Example() {
	w := httptest.NewRecorder()
	r, _ := http.NewRequest(http.MethodGet, "/", nil)
	r.Header.Set("Accept", "image/png")
	handler(w, r)
	fmt.Println(w.Body)

	w = httptest.NewRecorder()
	r.Header.Set("Accept", "image/*")
	handler(w, r)
	fmt.Println(w.Body)

	w = httptest.NewRecorder()
	r.Header.Set("Accept", "image/gif")
	handler(w, r)
	fmt.Println(w.Body)

	w = httptest.NewRecorder()
	r.Header.Set("Accept", "text/other")
	handler(w, r)
	fmt.Println(w.Body)

	w = httptest.NewRecorder()
	r.Header.Set("Accept", "image/png, text/other")
	handler(w, r)
	fmt.Println(w.Body)

	w = httptest.NewRecorder()
	r.Header.Set("Accept", "image/png;q=0.5, text/other")
	handler(w, r)
	fmt.Println(w.Body)

	w = httptest.NewRecorder()
	r.Header.Set("Accept", "*/*;q=0")
	handler(w, r)
	fmt.Println(w.Body)

	// Output:
	// png
	// png
	// image
	// other
	// png
	// other
	// none
}

Documentation

Full API docs can be found at:

https://pkg.go.dev/vimagination.zapto.org/httpaccept

FAQs

Package last updated on 26 Sep 2025

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