gitmyhub

req

Go ★ 4.8k updated 2d ago

Simple Go HTTP client with Black Magic

A Go HTTP client library that wraps the standard library with a cleaner API, handling JSON encoding and decoding, retries, auth, file uploads, and HTTP/2 and HTTP/3 automatically with far less boilerplate.

GoHTTP/2HTTP/3JSONREST APIssetup: easycomplexity 2/5

req is a Go library for making HTTP requests. Go includes a built-in HTTP client in its standard library, but it requires more setup code than many developers want to write for routine tasks like decoding JSON responses, adding authentication headers, retrying failed requests, or inspecting what was actually sent and received. req wraps that standard client with a cleaner interface that chains settings together and handles many common cases automatically.

The library automatically detects whether a server supports HTTP/2 or HTTP/3 and uses the best version available, though you can force a specific version if needed. It decodes response bodies into Go structs based on the content type without requiring extra parsing code, and it similarly encodes request bodies from structs automatically. Character encoding detection handles servers that return text in non-UTF-8 encodings, converting to UTF-8 so the application receives consistent text.

For debugging, the library can print the full contents of every request and response, including headers and body, to help identify problems during development. A performance tracing mode shows timing breakdowns for each phase of a request, such as connection time and time to first byte. These tools can be turned on globally during development and turned off for production with a single setting.

Other included features are automatic retry with configurable backoff, file upload and download with progress callbacks, HTTP Basic Auth, Bearer token auth, Digest auth, and middleware hooks that run before or after each request. The library is also designed so its transport layer can replace the transport in an existing standard-library HTTP client, which makes it possible to add these features to code that was already written without rewriting it from scratch.

Where it fits