Data URL Schemes in Golang
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-09-14 04:11:20 +01:00
.golangci.yml chore: change name, fix and modernise linter issues 2026-09-14 04:00:30 +01:00
.markdownlint-cli2.jsonc chore: change name, fix and modernise linter issues 2026-09-14 04:00:30 +01:00
dataurl.go chore: fix tabs 2026-09-14 04:11:20 +01:00
dataurl_test.go chore: fix tabs 2026-09-14 04:11:20 +01:00
doc.go chore: change name, fix and modernise linter issues 2026-09-14 03:59:57 +01:00
go.mod chore: change name, fix and modernise linter issues 2026-09-14 04:00:30 +01:00
lex.go chore: change name, fix and modernise linter issues 2026-09-14 03:59:57 +01:00
LICENSE chore: change name, fix and modernise linter issues 2026-09-14 03:59:57 +01:00
README.md chore: update readme 2026-09-14 04:01:53 +01:00
rfc2396.go chore: fix tabs 2026-09-14 04:11:20 +01:00
rfc2396_test.go chore: fix tabs 2026-09-14 04:11:20 +01:00

Data URL Schemes for Go

This package parses and generates Data URL Schemes for the Go language, according to RFC 2397.

Data URLs are small chunks of data commonly used in browsers to display inline data, typically like small images, or when you use the FileReader API of the browser.

Common use-cases:

  • generate a data URL out of a string, []byte, io.Reader for inclusion in HTML templates,
  • parse a data URL sent by a browser in a http.Handler, and do something with the data (save to disk, etc.)
  • ...

Install the package with:

go get rtlabs.tech/datauri

Usage

package main

import (
  "rtlabs.tech/datauri"
  "fmt"
)

func main() {
  dataURL, err := dataurl.DecodeString(`data:text/plain;charset=utf-8;base64,aGV5YQ==`)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Printf("content type: %s, data: %s\n", dataURL.MediaType.ContentType(), string(dataURL.Data))
  // Output: content type: text/plain, data: heya
}

From a http.Handler:

func handleDataURLUpload(w http.ResponseWriter, r *http.Request) {
  dataURL, err := dataurl.Decode(r.Body)
  defer r.Body.Close()
  if err != nil {
    http.Error(w, err.Error(), http.StatusBadRequest)
    return
  }
  if dataURL.ContentType() == "image/png" {
    ioutil.WriteFile("image.png", dataURL.Data, 0644)
  } else {
    http.Error(w, "not a png", http.StatusBadRequest)
  }
}

Code and Documentation Copyright © 1994-2026, Stephen Kapp and Reaper Technologies Limited. All Rights Reserved.