The http package provides lightweight helpers built on top of net/http. It is intended for shared service integrations where applications need common request headers, reusable content-type constants, simple request execution, and optional response decompression without hiding the standard library.
import gkhttp "github.com/raykavin/gokit/http"- common header name constants such as
Content-Type,Accept, andAuthorization - MIME type and
Cache-Controlconstants for common request scenarios - default header builders for JSON, form-encoded, and compressed requests
NewRequestWithContext()for context-aware requests with query params and an optional custom clientDecompressResponse()forgzip,deflate,br, andzstdencoded responsesAcceptEncodingAllto advertise support for every encoding handled byDecompressResponse()
NewRequestWithContext(): builds, executes, and reads an HTTP request in one callDecompressResponse(): wraps a response body with the appropriate decompression readerDefaultJSONHeaders(),DefaultFormHeaders(), andDefaultCompressedHeaders(): return ready-to-use header maps for common casesHeader*,MIME*, andCacheControl*constants: avoid repeating common header and content valuesAcceptEncodingAll:Accept-Encodingvalue covering all supported decompression formats
package main
import (
"context"
"fmt"
"log"
stdhttp "net/http"
"time"
gkhttp "github.com/raykavin/gokit/http"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
headers := gkhttp.DefaultJSONHeaders()
headers[gkhttp.HeaderAuthorization] = "Bearer <token>"
body, status, err := gkhttp.NewRequestWithContext(
ctx,
stdhttp.MethodGet,
"https://api.example.com/users",
map[string]string{
"page": "1",
"limit": "20",
},
headers,
nil,
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("status=%d body=%s\n", status, body)
}Use DecompressResponse() when you are working directly with *http.Response and need to handle compressed payloads yourself.
package main
import (
"context"
"io"
"log"
stdhttp "net/http"
"time"
gkhttp "github.com/raykavin/gokit/http"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, err := stdhttp.NewRequestWithContext(ctx, stdhttp.MethodGet, "https://example.com/data", nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set(gkhttp.HeaderAcceptEncoding, gkhttp.AcceptEncodingAll)
resp, err := (&stdhttp.Client{Timeout: 5 * time.Second}).Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
reader, err := gkhttp.DecompressResponse(resp)
if err != nil {
log.Fatal(err)
}
defer reader.Close()
payload, err := io.ReadAll(reader)
if err != nil {
log.Fatal(err)
}
log.Printf("status=%d bytes=%d", resp.StatusCode, len(payload))
}- importing the package with an alias such as
gkhttphelps avoid confusion with the standard librarynet/http NewRequestWithContext()reads the full response body and returns it together with the HTTP status code- the package-level default client does not define a timeout, so callers should pass a context deadline or a custom client when needed
DefaultCompressedHeaders()returnsAccept-Encoding: gzip, deflate, br, zstdDecompressResponse()does not execute requests by itself; it only wraps an existing*http.Response