Documentation
¶
Overview ¶
Package requests provides a simple and easy-to-use HTTP client library for Go.
Index ¶
- Constants
- Variables
- func AcquireSession() client.Session
- func DefaultSession() client.Session
- func GetBytes(baseURL string, opts ...client.RequestOption) ([]byte, error)
- func GetString(baseURL string, opts ...client.RequestOption) (string, error)
- func GetTransport(enableHTTP2 bool) *http.Transport
- func IsHTTP2Enabled() bool
- func NewSession() client.Session
- func PutTransport(transport *http.Transport)
- func ReleaseSession(sess client.Session)
- func SetHTTP2Enabled(enabled bool)
- type BasicAuth
- type Client
- type ConnectionError
- type DecodeError
- type EncodeError
- type HTTPClient
- type Handler
- type Hooks
- type Method
- type Middleware
- type MiddlewareChain
- type MiddlewareFunc
- type Request
- type RequestBuilder
- type RequestError
- type RequestOption
- type Response
- func Delete(baseURL string, opts ...client.RequestOption) (*Response, error)
- func Get(baseURL string, opts ...client.RequestOption) (*Response, error)
- func Head(baseURL string, opts ...client.RequestOption) (*Response, error)
- func Options(baseURL string, opts ...client.RequestOption) (*Response, error)
- func Patch(baseURL string, body any, opts ...client.RequestOption) (*Response, error)
- func Post(baseURL string, body any, opts ...client.RequestOption) (*Response, error)
- func Put(baseURL string, body any, opts ...client.RequestOption) (*Response, error)
- type ResponseError
- type Result
- func DeleteJSON[T any](baseURL string, opts ...client.RequestOption) (Result[T], error)
- func DoJSON[T any](b *RequestBuilder) (Result[T], error)
- func DoXML[T any](b *RequestBuilder) (Result[T], error)
- func GetJSON[T any](baseURL string, opts ...client.RequestOption) (Result[T], error)
- func PatchJSON[T any](baseURL string, data any, opts ...client.RequestOption) (Result[T], error)
- func PostJSON[T any](baseURL string, data any, opts ...client.RequestOption) (Result[T], error)
- func PutJSON[T any](baseURL string, data any, opts ...client.RequestOption) (Result[T], error)
- type RetryError
- type RetryExecutor
- type RetryPolicy
- type Session
- type TimeoutError
Constants ¶
const ( MethodGet = client.MethodGet MethodPost = client.MethodPost MethodPut = client.MethodPut MethodDelete = client.MethodDelete MethodPatch = client.MethodPatch MethodHead = client.MethodHead MethodOptions = client.MethodOptions MethodConnect = client.MethodConnect MethodTrace = client.MethodTrace )
HTTP methods as constants - re-exported from client package.
Variables ¶
var ( WithTimeout = client.WithTimeout // WithHeader adds a single header to the request. WithHeader = client.WithHeader // WithHeaders adds multiple headers to the request. WithHeaders = client.WithHeaders // WithQuery adds a single query parameter. WithQuery = client.WithQuery // WithQueryParams adds multiple query parameters. WithQueryParams = client.WithQueryParams // WithBasicAuth adds basic authentication. WithBasicAuth = client.WithBasicAuth // WithBearerToken adds bearer token authentication. WithBearerToken = client.WithBearerToken // WithContext sets the request context. WithContext = client.WithContext // WithContentType sets the Content-Type header. WithContentType = client.WithContentType // WithAccept sets the Accept header. WithAccept = client.WithAccept // ============================================================================ // Middleware and Hooks // ============================================================================ // NewMiddlewareChain creates a new middleware chain. NewMiddlewareChain = client.NewMiddlewareChain // NewHooks creates a new Hooks instance. NewHooks = client.NewHooks // NewMetricsHook creates a new metrics hook. NewMetricsHook = client.NewMetricsHook // HeaderMiddleware creates middleware that adds headers. HeaderMiddleware = client.HeaderMiddleware // HooksMiddleware creates middleware from hooks. HooksMiddleware = client.HooksMiddleware // ============================================================================ // Retry // ============================================================================ // NewRetryExecutor creates a new retry executor. NewRetryExecutor = client.NewRetryExecutor // NoRetryPolicy returns a policy that never retries. NoRetryPolicy = client.NoRetryPolicy // LinearRetryPolicy returns a policy with linear backoff. LinearRetryPolicy = client.LinearRetryPolicy // ExponentialRetryPolicy returns a policy with exponential backoff. ExponentialRetryPolicy = client.ExponentialRetryPolicy // RetryOn5xx returns a condition that retries on 5xx errors. RetryOn5xx = client.RetryOn5xx // RetryOnNetworkError returns a condition that retries on network errors. RetryOnNetworkError = client.RetryOnNetworkError // RetryOnStatusCodes returns a condition that retries on specific status codes. RetryOnStatusCodes = client.RetryOnStatusCodes // CombineRetryConditions combines multiple retry conditions. CombineRetryConditions = client.CombineRetryConditions )
============================================================================ Request Options ============================================================================ WithTimeout sets the request timeout.
var ( IsTimeout = client.IsTimeout // IsConnectionError checks if an error is a connection error. IsConnectionError = client.IsConnectionError // IsResponseError checks if an error is a response error. IsResponseError = client.IsResponseError // IsTemporary checks if an error is temporary. IsTemporary = client.IsTemporary )
============================================================================ Error Helpers ============================================================================ IsTimeout checks if an error is a timeout error.
Functions ¶
func AcquireSession ¶ added in v0.1.0
AcquireSession gets a Session from the pool. Remember to call ReleaseSession when done.
func DefaultSession ¶ added in v0.1.0
DefaultSession returns the default session.
func GetBytes ¶ added in v0.1.0
func GetBytes(baseURL string, opts ...client.RequestOption) ([]byte, error)
GetBytes sends a GET request and returns the response body as bytes.
func GetString ¶ added in v0.1.0
func GetString(baseURL string, opts ...client.RequestOption) (string, error)
============================================================================ Convenience Functions ============================================================================ GetString sends a GET request and returns the response body as a string.
func GetTransport ¶ added in v0.1.0
GetTransport returns a transport from the pool.
func IsHTTP2Enabled ¶ added in v0.1.0
func IsHTTP2Enabled() bool
IsHTTP2Enabled returns the global HTTP/2 enabled state.
func NewSession ¶
NewSession creates a new Session with default settings.
func PutTransport ¶ added in v0.1.0
PutTransport returns a transport to the pool.
func ReleaseSession ¶ added in v0.1.0
ReleaseSession returns a Session to the pool.
func SetHTTP2Enabled ¶
func SetHTTP2Enabled(enabled bool)
SetHTTP2Enabled sets the global HTTP/2 enabled state.
Types ¶
type Client ¶ added in v0.1.0
Client is the core HTTP client interface. It provides Do, DoWithContext, and Clone methods.
type HTTPClient ¶ added in v0.1.0
type HTTPClient = client.HTTPClient
HTTPClient is the interface for executing HTTP requests.
type Handler ¶ added in v0.1.0
Handler is a function that processes a request and returns a response.
type Middleware ¶ added in v0.1.0
type Middleware = client.Middleware
Middleware is the interface for request/response middleware.
type MiddlewareChain ¶ added in v0.1.0
type MiddlewareChain = client.MiddlewareChain
MiddlewareChain manages a chain of middleware.
type MiddlewareFunc ¶ added in v0.2.0
type MiddlewareFunc = client.MiddlewareFunc
MiddlewareFunc is a function adapter for Middleware interface.
type Request ¶ added in v0.1.0
Request is an alias for client.Request - the unified request type. Use NewRequestBuilder or convenience constructors (NewGet, NewPost, etc.) to create requests.
type RequestBuilder ¶ added in v0.1.0
type RequestBuilder = client.RequestBuilder
RequestBuilder provides a fluent interface for building HTTP requests.
func NewDeleteBuilder ¶ added in v0.1.0
func NewDeleteBuilder(rawURL string) *RequestBuilder
NewDelete creates a new DELETE request builder.
func NewGet ¶ added in v0.1.0
func NewGet(rawURL string) *RequestBuilder
NewGet creates a new GET request builder.
func NewPatch ¶ added in v0.1.0
func NewPatch(rawURL string) *RequestBuilder
NewPatch creates a new PATCH request builder.
func NewPost ¶ added in v0.1.0
func NewPost(rawURL string) *RequestBuilder
NewPost creates a new POST request builder.
func NewPut ¶ added in v0.1.0
func NewPut(rawURL string) *RequestBuilder
NewPut creates a new PUT request builder.
func NewRequestBuilder ¶ added in v0.1.0
============================================================================ RequestBuilder Constructors ============================================================================ NewRequestBuilder creates a new RequestBuilder with the specified method and URL.
type RequestOption ¶ added in v0.1.0
type RequestOption = client.RequestOption
RequestOption is a function that modifies request configuration.
type Response ¶ added in v0.1.0
Response represents an HTTP response. This is the unified response type used throughout the library.
func Delete ¶
func Delete(baseURL string, opts ...client.RequestOption) (*Response, error)
Delete sends a DELETE request and returns the response.
func Get ¶
func Get(baseURL string, opts ...client.RequestOption) (*Response, error)
============================================================================ Basic HTTP Methods (with variadic options) ============================================================================ Get sends a GET request and returns the response.
func Head ¶ added in v0.1.0
func Head(baseURL string, opts ...client.RequestOption) (*Response, error)
Head sends a HEAD request and returns the response.
func Options ¶ added in v0.1.0
func Options(baseURL string, opts ...client.RequestOption) (*Response, error)
Options sends an OPTIONS request and returns the response.
func Patch ¶
func Patch(baseURL string, body any, opts ...client.RequestOption) (*Response, error)
Patch sends a PATCH request with optional body and returns the response.
type Result ¶ added in v0.1.0
Type aliases for client package types - allows users to import only the main package Result wraps both parsed response data and response metadata. Use Data() to access the parsed data and Response() for metadata.
func DeleteJSON ¶ added in v0.1.0
DeleteJSON sends a DELETE request and returns Result[T].
func DoJSON ¶ added in v0.1.0
============================================================================ Generic Request Execution ============================================================================ DoJSON executes a request builder and parses JSON response into Result[T].
func DoXML ¶ added in v0.1.0
DoXML executes a request builder and parses XML response into Result[T].
func GetJSON ¶ added in v0.1.0
============================================================================ Generic HTTP Methods - return (Result[T], error) ============================================================================ GetJSON sends a GET request and returns Result[T] with parsed JSON response.
func PatchJSON ¶ added in v0.1.0
PatchJSON sends a PATCH request with JSON body and returns Result[T].
type RetryExecutor ¶ added in v0.1.0
type RetryExecutor = client.RetryExecutor
RetryExecutor executes requests with retry logic.
type RetryPolicy ¶ added in v0.1.0
type RetryPolicy = client.RetryPolicy
RetryPolicy defines retry behavior.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package codec provides encoder/decoder interfaces and implementations.
|
Package codec provides encoder/decoder interfaces and implementations. |
|
docs
|
|
|
examples
Package examples demonstrates context usage patterns with the requests library.
|
Package examples demonstrates context usage patterns with the requests library. |
|
internal
|
|
|
client
Package client provides the core HTTP client interfaces and implementations.
|
Package client provides the core HTTP client interfaces and implementations. |