Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

12 Commits

Repository files navigation

chid

A small Go package for extracting and validating integer IDs from HTTP requests, designed for use with the chi/v5 router, but can be adapted for other frameworks.

Features

  • Extracts integer IDs from HTTP requests (URL params or form values)
  • Supports int16, int32, and int64 types
  • Customizable key and source via options
  • Clear error handling for invalid or missing IDs

Installation

go get github.com/kaatinga/chid

Usage

import (
"net/http""github.com/kaatinga/chid"
)
// Example handler using chi routerfunchandler(w http.ResponseWriter, r*http.Request) {
id, err:= chid.GetID[int64](r)
iferr!=nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Use id (int64)
}

Customization

You can customize how the ID is extracted using options:

  • chid.WithIDKey(key string): Use a custom key instead of the default "id"
  • chid.WithFormOrQuery(): Extract from form or query values instead of URL params

Example:

id, err:=chid.GetID[int32](r, chid.WithIDKey("user_id"), chid.WithFormOrQuery())

Error Handling

GetID returns descriptive errors for:

  • Unsupported ID type
  • Unable to parse the ID value
  • ID is zero or below zero

License

MIT

About

id request parser for chi/v5 package

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages