httputil

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package httputil provides a thin HTTP+JSON helper for providers.

Index

Constants

View Source
const DefaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"

DefaultUserAgent is sent on every request unless the caller explicitly sets a User-Agent header. Go's default "Go-http-client/1.1" gets blocked by Cloudflare, so we use a real browser UA.

Variables

This section is empty.

Functions

func GetJSON

func GetJSON(url string, headers map[string]string, timeout time.Duration, dst any) error

GetJSON performs a GET request and decodes the JSON response into dst.

func PostJSON

func PostJSON(url string, headers map[string]string, payload any, timeout time.Duration, dst any) error

PostJSON performs a POST request with a JSON body and decodes the response.

func Truncate

func Truncate(s string, n int) string

Truncate shortens a string to n chars.

Types

type Error

type Error struct {
	Status     int
	StatusText string
	Body       string
	URL        string
	Headers    http.Header
}

Error is returned for non-2xx HTTP responses.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Header

func (e *Error) Header(name string) string

Header returns a response header value (case-insensitive).

func (*Error) RetryAfter added in v0.5.0

func (e *Error) RetryAfter() time.Time

RetryAfter returns the absolute time the server asked us to wait until before retrying, parsed from common rate-limit / server-busy response headers. Returns zero-time when no recognized hint is present or the status code isn't one that should carry one.

Recognized headers, in order of preference:

  • Retry-After (RFC 7231: seconds OR HTTP date)
  • x-ratelimit-reset (Unix timestamp OR seconds-from-now)
  • anthropic-ratelimit-requests-reset (ISO 8601)
  • anthropic-ratelimit-tokens-reset (ISO 8601)

Jump to

Keyboard shortcuts

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