https://github.com/joeychilson/httputils
A library of helper functions for Go's net/http package.
https://github.com/joeychilson/httputils
go golang http utils
Last synced: about 1 year ago
JSON representation
A library of helper functions for Go's net/http package.
- Host: GitHub
- URL: https://github.com/joeychilson/httputils
- Owner: joeychilson
- License: mit
- Created: 2023-03-16T11:58:33.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-16T12:00:25.000Z (about 3 years ago)
- Last Synced: 2025-01-16T13:19:36.150Z (about 1 year ago)
- Topics: go, golang, http, utils
- Language: Go
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# httputils
A library of helper functions for Go's `net/http` package.
## Installation
```bash
go get github.com/joeychilon/httputils
```
## Usage
### Query Parameters
The library provides functions for parsing query parameters in HTTP requests:
- `QueryString(r *http.Request, key string) string`: returns a string value for a query parameter.
- `QueryStrings(r *http.Request, key string) []string`: returns a slice of strings for a query parameter.
- `QueryInt(r *http.Request, key string) int`: returns an int value for a query parameter.
- `QueryInts(r *http.Request, key string) []int`: returns a slice of ints for a query parameter.
- `QueryBool(r *http.Request, key string) bool`: returns a bool value for a query parameter.
Here's an example of how to use these functions:
```go
func myHandler(w http.ResponseWriter, r *http.Request) {
// Get a string query parameter
// key=foo => name = "foo"
name := httputils.QueryString(r, "name")
// Get a slice of strings query parameter
// key=foo,bar => names = ["foo", "bar"]
names := httputils.QueryStrings(r, "names")
// Get an int query parameter
// key=1 => id = 1
id := httputils.QueryInt(r, "id")
// Get a slice of ints query parameter
// key=1,2,3 => ids = [1, 2, 3]
ids := httputils.QueryInts(r, "ids")
// Get a bool query parameter
// key=true => enabled = true
enabled := httputils.QueryBool(r, "enabled")
}
```
### JSON Responses
The library provides a function for writing JSON responses to the `http.ResponseWriter`:
`WriteJSON(w http.ResponseWriter, statusCode int, data any)`: writes a JSON response with the given status code and data.
Here's an example of how to use this function:
```go
func myHandler(w http.ResponseWriter, r *http.Request) {
// Get some data...
data := getSomeData()
// Write a JSON response
httputils.WriteJSON(w, http.StatusOK, data)
}
```