httputils

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httputils defines utilities for making HTTP calls. Includes functions for returning the entire response as a result.Result[*http.Response] or just the body as a result.Result[string] or the result.Result[T] where the response body is unmarashaled from JSON into a T.

In addition to the response body, if the type T implements the ResponseHeaderSetter interface then the response headers will be set on the T before it is returned. Note that the type used with DoAsJSON must match the receiver of the SetResponseHeaders method for that method to be called.

StatusCodes greater than 299 are converted to errors.

Debug output of the request and response can be enabled by the GGDK_HTTP_DEBUG environment variable. Valid values are anything that strconv.ParseBool accepts as well as the value "full". A value of true enables basic debug output, while a value of "full" enables more detailed debug output including the request and response headers and bodies. By default, this output is written to stderr that functions are provided to change this destination to any io.Writer.

Example:

type data struct {
	Field1 string `json:"field1"`
	Field2 string `json:"field2"`
	headers http.Header
}
func (d *data) SetResponseHeaders(headers http.Header) *data {
	d.headers = headers
	return d
}
func main() {
	req, err := http.NewRequest(http.MethodGet, "https://example.com", nil)
	if err != nil {
		log.Fatal(err)
	}
	d := httputils.DoAsJSON[*data](http.DefaultClient, req)
}

Package httputils (see doc.go for package documentation). The bodyclose linter cannot follow the response body through the result chain to ensure it is closed, so it is disabled for this file. nolint:bodyclose

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

Do executes the HTTP request using the given client and returns a result containing the response without further processing. StatusCodes greater than 299 are converted to errors with the response body in the error message. The caller is responsible for closing the response body.

func DoAsJSON

func DoAsJSON[T any](c *http.Client, req *http.Request) result.Result[T]

DoAsJSON executes the HTTP request using the given client and returns a Result containing the unmarshaled response of type T. StatusCodes greater than 299 are converted to errors with the response body in the error message.

func DoAsString

func DoAsString(c *http.Client, req *http.Request) result.Result[string]

DoAsString executes the HTTP request using the given client and returns a Result containing the response body as a string. StatusCodes greater than 299 are converted to errors with the response body in the error message.

func DoNoResponse added in v0.4.0

func DoNoResponse(c *http.Client, req *http.Request) error

DoNoResponse is deprecated and will be removed. Use DoNoResponseBody instead.

func DoNoResponseBody added in v0.4.1

func DoNoResponseBody(c *http.Client, req *http.Request) error

DoNoResponseBody executes the HTTP request using the given client and returns an error if the request fails (status code > 299) or nil if it succeeds. The response body is closed before returning.

func SetDebug

func SetDebug(level string)

SetDebug sets the debug level based on the given string. It accepts "disabled", "enabled", "full", or any value that strconv.ParseBool can parse (1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False). If the input cannot be parsed, the debug level defaults to disabled.

func SetDebugOutput

func SetDebugOutput(w io.Writer)

SetDebugOutput sets the output destination for HTTP debug information.

Types

type ResponseHeaderSetter

type ResponseHeaderSetter[T any] interface {
	SetResponseHeaders(http.Header) T //nolint:inamedparam
}

ResponseHeaderSetter defines an interface for types that can have response headers set on them. This is used to provide response headers to the caller if the type returned by DoAsJSON implements this interface. The SetResponseHeaders method returns a T so that the method can have a value receiver. If the receiver is a pointer, then the type used with DoAsJson should be a pointer type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL