Skip to content

Commit d231b17

Browse files
committed
docs: cleanup
1 parent e613fcc commit d231b17

30 files changed

Lines changed: 412 additions & 93 deletions

File tree

README.md

Lines changed: 97 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ It keeps req's power and escape hatches, while making the 90% use case feel effo
1414
<a href="https://goreportcard.com/report/github.com/goforj/httpx"><img src="https://goreportcard.com/badge/github.com/goforj/httpx" alt="Go Report Card"></a>
1515
<a href="https://codecov.io/gh/goforj/httpx" ><img src="https://codecov.io/gh/goforj/httpx/graph/badge.svg?token=R5O7LYAD4B"/></a>
1616
<!-- test-count:embed:start -->
17-
<img src="https://img.shields.io/badge/tests-197-brightgreen" alt="Tests">
17+
<img src="https://img.shields.io/badge/tests-203-brightgreen" alt="Tests">
1818
<!-- test-count:embed:end -->
1919
</p>
2020

@@ -102,8 +102,7 @@ Browser profiles provide a simple way to match common client behavior without ex
102102
Internally, profiles may apply transport and protocol settings as needed, but those details are intentionally abstracted.
103103

104104
```go
105-
c := httpx.New(httpx.AsChrome())
106-
_ = c
105+
_ = httpx.New(httpx.AsChrome())
107106
```
108107

109108
## Use Any req Feature
@@ -176,7 +175,7 @@ They are compiled by `example_compile_test.go` to keep docs and code in sync.
176175
| **Debugging** | [Dump](#dump) [DumpAll](#dumpall) [DumpEachRequest](#dumpeachrequest) [DumpEachRequestTo](#dumpeachrequestto) [DumpTo](#dumpto) [DumpToFile](#dumptofile) [EnableDump](#enabledump) [Trace](#trace) [TraceAll](#traceall) |
177176
| **Download Options** | [OutputFile](#outputfile) |
178177
| **Errors** | [Error](#error) |
179-
| **Request Composition** | [Body](#body) [Form](#form) [Header](#header) [JSON](#json) [Path](#path) [Query](#query) [UserAgent](#useragent) |
178+
| **Request Composition** | [Body](#body) [Form](#form) [Header](#header) [Headers](#headers) [JSON](#json) [Path](#path) [Paths](#paths) [Queries](#queries) [Query](#query) [UserAgent](#useragent) |
180179
| **Request Control** | [Before](#before) [Timeout](#timeout) |
181180
| **Requests** | [Delete](#delete) [Do](#do) [Get](#get) [Head](#head) [Options](#options) [Patch](#patch) [Post](#post) [Put](#put) |
182181
| **Requests (Context)** | [DeleteCtx](#deletectx) [GetCtx](#getctx) [HeadCtx](#headctx) [OptionsCtx](#optionsctx) [PatchCtx](#patchctx) [PostCtx](#postctx) [PutCtx](#putctx) |
@@ -276,8 +275,7 @@ httpx.Dump(res) // dumps map[string]any
276275
AsChrome applies the Chrome browser profile (headers including User-Agent, TLS, and HTTP/2 behavior).
277276

278277
```go
279-
c := httpx.New(httpx.AsChrome())
280-
_ = c
278+
_ = httpx.New(httpx.AsChrome())
281279
```
282280

283281
### <a id="asfirefox"></a>AsFirefox
@@ -301,17 +299,15 @@ httpx.Dump(res) // dumps map[string]any
301299
AsMobile applies a mobile Chrome-like profile (headers including User-Agent, TLS, and HTTP/2 behavior).
302300

303301
```go
304-
c := httpx.New(httpx.AsMobile())
305-
_ = c
302+
_ = httpx.New(httpx.AsMobile())
306303
```
307304

308305
### <a id="assafari"></a>AsSafari
309306

310307
AsSafari applies the Safari browser profile (headers including User-Agent, TLS, and HTTP/2 behavior).
311308

312309
```go
313-
c := httpx.New(httpx.AsSafari())
314-
_ = c
310+
_ = httpx.New(httpx.AsSafari())
315311
```
316312

317313
## Client
@@ -326,7 +322,7 @@ New creates a client with opinionated defaults and optional overrides.
326322

327323
```go
328324
var buf bytes.Buffer
329-
c := httpx.New(httpx.
325+
_ = httpx.New(httpx.
330326
BaseURL("https://httpbin.org").
331327
Timeout(5*time.Second).
332328
Header("X-Trace", "1").
@@ -356,7 +352,6 @@ c := httpx.New(httpx.
356352
}).
357353
RetryHook(func(_ *req.Response, _ error) {}),
358354
)
359-
_ = c
360355
```
361356

362357
### <a id="raw"></a>Raw
@@ -721,6 +716,41 @@ httpx.Dump(res) // dumps map[string]any
721716
// }
722717
```
723718

719+
### <a id="headers"></a>Headers
720+
721+
Headers sets multiple headers on a request or client.
722+
723+
```go
724+
// Apply to all requests
725+
c := httpx.New(httpx.Headers(map[string]string{
726+
"X-Trace": "1",
727+
"Accept": "application/json",
728+
}))
729+
res, err := httpx.Get[map[string]any](c, "https://httpbin.org/headers")
730+
_ = err
731+
httpx.Dump(res) // dumps map[string]any
732+
// #map[string]interface {} {
733+
// headers => #map[string]interface {} {
734+
// Accept => "application/json" #string
735+
// X-Trace => "1" #string
736+
// }
737+
// }
738+
739+
// Apply to a single request
740+
res, err = httpx.Get[map[string]any](c, "https://httpbin.org/headers", httpx.Headers(map[string]string{
741+
"X-Trace": "1",
742+
"Accept": "application/json",
743+
}))
744+
_ = err
745+
httpx.Dump(res) // dumps map[string]any
746+
// #map[string]interface {} {
747+
// headers => #map[string]interface {} {
748+
// Accept => "application/json" #string
749+
// X-Trace => "1" #string
750+
// }
751+
// }
752+
```
753+
724754
### <a id="json"></a>JSON
725755

726756
JSON sets the request body as JSON.
@@ -759,6 +789,43 @@ httpx.Dump(res) // dumps map[string]any
759789
// }
760790
```
761791

792+
### <a id="paths"></a>Paths
793+
794+
Paths sets multiple path parameters from a map.
795+
796+
```go
797+
c := httpx.New()
798+
res, err := httpx.Get[map[string]any](c, "https://httpbin.org/anything/{org}/users/{id}", httpx.Paths(map[string]any{
799+
"org": "goforj",
800+
"id": 7,
801+
}))
802+
_ = err
803+
httpx.Dump(res) // dumps map[string]any
804+
// #map[string]interface {} {
805+
// url => "https://httpbin.org/anything/goforj/users/7" #string
806+
// }
807+
```
808+
809+
### <a id="queries"></a>Queries
810+
811+
Queries sets query parameters from a map.
812+
813+
```go
814+
c := httpx.New()
815+
res, err := httpx.Get[map[string]any](c, "https://httpbin.org/get", httpx.Queries(map[string]string{
816+
"q": "search",
817+
"ok": "1",
818+
}))
819+
_ = err
820+
httpx.Dump(res) // dumps map[string]any
821+
// #map[string]interface {} {
822+
// args => #map[string]interface {} {
823+
// ok => "1" #string
824+
// q => "search" #string
825+
// }
826+
// }
827+
```
828+
762829
### <a id="query"></a>Query
763830

764831
Query adds query parameters as key/value pairs.
@@ -1168,6 +1235,7 @@ httpx.Dump(res) // dumps UpdateUserResponse
11681235
### <a id="retrybackoff"></a>RetryBackoff
11691236

11701237
RetryBackoff sets a capped exponential backoff retry interval for a request.
1238+
Overrides the req default interval (fixed 100ms) with jittered backoff.
11711239

11721240
```go
11731241
// Apply to all requests
@@ -1191,6 +1259,7 @@ httpx.Dump(res) // dumps map[string]any
11911259
### <a id="retrycondition"></a>RetryCondition
11921260

11931261
RetryCondition sets the retry condition for a request.
1262+
Overrides the default behavior (retry only when a request error occurs).
11941263

11951264
```go
11961265
// Apply to all requests
@@ -1214,6 +1283,9 @@ httpx.Dump(res) // dumps map[string]any
12141283
### <a id="retrycount"></a>RetryCount
12151284

12161285
RetryCount enables retry for a request and sets the maximum retry count.
1286+
Default behavior from req: retries are disabled (count = 0). When enabled,
1287+
retries happen only on request errors unless RetryCondition is set, and the
1288+
default interval is a fixed 100ms between attempts.
12171289

12181290
```go
12191291
// Apply to all requests
@@ -1237,6 +1309,7 @@ httpx.Dump(res) // dumps map[string]any
12371309
### <a id="retryfixedinterval"></a>RetryFixedInterval
12381310

12391311
RetryFixedInterval sets a fixed retry interval for a request.
1312+
Overrides the req default interval (fixed 100ms).
12401313

12411314
```go
12421315
// Apply to all requests
@@ -1260,6 +1333,7 @@ httpx.Dump(res) // dumps map[string]any
12601333
### <a id="retryhook"></a>RetryHook
12611334

12621335
RetryHook registers a retry hook for a request.
1336+
Runs before each retry attempt; no hooks are configured by default.
12631337

12641338
```go
12651339
// Apply to all requests
@@ -1283,6 +1357,7 @@ httpx.Dump(res) // dumps map[string]any
12831357
### <a id="retryinterval"></a>RetryInterval
12841358

12851359
RetryInterval sets a custom retry interval function for a request.
1360+
Overrides the req default interval (fixed 100ms).
12861361

12871362
```go
12881363
// Apply to all requests
@@ -1312,12 +1387,12 @@ httpx.Dump(res) // dumps map[string]any
13121387
### <a id="retry"></a>Retry
13131388

13141389
Retry applies a custom retry configuration to the client.
1390+
Defaults remain in effect unless the callback modifies them.
13151391

13161392
```go
1317-
c := httpx.New(httpx.Retry(func(rc *req.Client) {
1393+
_ = httpx.New(httpx.Retry(func(rc *req.Client) {
13181394
rc.SetCommonRetryCount(2)
13191395
}))
1320-
_ = c
13211396
```
13221397

13231398
## Upload Options
@@ -1466,70 +1541,62 @@ httpx.Dump(res) // dumps map[string]any
14661541
TLSFingerprint applies a TLS fingerprint preset.
14671542

14681543
```go
1469-
c := httpx.New(httpx.TLSFingerprint(httpx.TLSFingerprintChromeKind))
1470-
_ = c
1544+
_ = httpx.New(httpx.TLSFingerprint(httpx.TLSFingerprintChromeKind))
14711545
```
14721546

14731547
### <a id="tlsfingerprintandroid"></a>TLSFingerprintAndroid
14741548

14751549
TLSFingerprintAndroid applies the Android TLS fingerprint preset.
14761550

14771551
```go
1478-
c := httpx.New(httpx.TLSFingerprintAndroid())
1479-
_ = c
1552+
_ = httpx.New(httpx.TLSFingerprintAndroid())
14801553
```
14811554

14821555
### <a id="tlsfingerprintchrome"></a>TLSFingerprintChrome
14831556

14841557
TLSFingerprintChrome applies the Chrome TLS fingerprint preset.
14851558

14861559
```go
1487-
c := httpx.New(httpx.TLSFingerprintChrome())
1488-
_ = c
1560+
_ = httpx.New(httpx.TLSFingerprintChrome())
14891561
```
14901562

14911563
### <a id="tlsfingerprintedge"></a>TLSFingerprintEdge
14921564

14931565
TLSFingerprintEdge applies the Edge TLS fingerprint preset.
14941566

14951567
```go
1496-
c := httpx.New(httpx.TLSFingerprintEdge())
1497-
_ = c
1568+
_ = httpx.New(httpx.TLSFingerprintEdge())
14981569
```
14991570

15001571
### <a id="tlsfingerprintfirefox"></a>TLSFingerprintFirefox
15011572

15021573
TLSFingerprintFirefox applies the Firefox TLS fingerprint preset.
15031574

15041575
```go
1505-
c := httpx.New(httpx.TLSFingerprintFirefox())
1506-
_ = c
1576+
_ = httpx.New(httpx.TLSFingerprintFirefox())
15071577
```
15081578

15091579
### <a id="tlsfingerprintios"></a>TLSFingerprintIOS
15101580

15111581
TLSFingerprintIOS applies the iOS TLS fingerprint preset.
15121582

15131583
```go
1514-
c := httpx.New(httpx.TLSFingerprintIOS())
1515-
_ = c
1584+
_ = httpx.New(httpx.TLSFingerprintIOS())
15161585
```
15171586

15181587
### <a id="tlsfingerprintrandomized"></a>TLSFingerprintRandomized
15191588

15201589
TLSFingerprintRandomized applies a randomized TLS fingerprint preset.
15211590

15221591
```go
1523-
c := httpx.New(httpx.TLSFingerprintRandomized())
1524-
_ = c
1592+
_ = httpx.New(httpx.TLSFingerprintRandomized())
15251593
```
15261594

15271595
### <a id="tlsfingerprintsafari"></a>TLSFingerprintSafari
15281596

15291597
TLSFingerprintSafari applies the Safari TLS fingerprint preset.
15301598

15311599
```go
1532-
c := httpx.New(httpx.TLSFingerprintSafari())
1533-
_ = c
1600+
_ = httpx.New(httpx.TLSFingerprintSafari())
15341601
```
15351602
<!-- api:embed:end -->

client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Client struct {
3131
// Example: configure all client options
3232
//
3333
// var buf bytes.Buffer
34-
// c := httpx.New(httpx.
34+
// _ = httpx.New(httpx.
3535
// BaseURL("https://httpbin.org").
3636
// Timeout(5*time.Second).
3737
// Header("X-Trace", "1").
@@ -61,7 +61,6 @@ type Client struct {
6161
// }).
6262
// RetryHook(func(_ *req.Response, _ error) {}),
6363
// )
64-
// _ = c
6564
func New(opts ...Option) *Client {
6665
c := &Client{
6766
req: req.C().SetTimeout(defaultTimeout).SetUserAgent(defaultUserAgent),

examples/aschrome/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ func main() {
99
// AsChrome applies the Chrome browser profile (headers including User-Agent, TLS, and HTTP/2 behavior).
1010

1111
// Example: use a Chrome profile
12-
c := httpx.New(httpx.AsChrome())
13-
_ = c
12+
_ = httpx.New(httpx.AsChrome())
1413
}

examples/asmobile/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ func main() {
99
// AsMobile applies a mobile Chrome-like profile (headers including User-Agent, TLS, and HTTP/2 behavior).
1010

1111
// Example: use a mobile profile
12-
c := httpx.New(httpx.AsMobile())
13-
_ = c
12+
_ = httpx.New(httpx.AsMobile())
1413
}

examples/assafari/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ func main() {
99
// AsSafari applies the Safari browser profile (headers including User-Agent, TLS, and HTTP/2 behavior).
1010

1111
// Example: use a Safari profile
12-
c := httpx.New(httpx.AsSafari())
13-
_ = c
12+
_ = httpx.New(httpx.AsSafari())
1413
}

examples/get/main.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,30 @@ package main
66
import "github.com/goforj/httpx"
77

88
func main() {
9-
type Request struct {
10-
Uuid string `json:"uuid"`
9+
// Get issues a GET request using the provided client.
10+
11+
// Example: bind to a struct
12+
type GetResponse struct {
13+
URL string `json:"url"`
1114
}
1215

13-
// client
1416
c := httpx.New()
15-
16-
// request, binds result to Request
17-
res, _ := httpx.Get[Request](c, "https://httpbin.org/uuid")
18-
17+
res, err := httpx.Get[GetResponse](c, "https://httpbin.org/get")
18+
if err != nil {
19+
return
20+
}
1921
httpx.Dump(res)
20-
// #main.Request {
21-
// +Uuid => "6101eccc-8f59-444f-9ccc-9c39a85d5da5" #string
22+
// #GetResponse {
23+
// URL => "https://httpbin.org/get" #string
24+
// }
25+
26+
// Example: bind to a string body
27+
resText, err := httpx.Get[string](c, "https://httpbin.org/uuid")
28+
if err != nil {
29+
return
30+
}
31+
println(resText) // dumps string
32+
// {
33+
// "uuid": "becbda6d-9950-4966-ae23-0369617ba065"
2234
// }
2335
}

0 commit comments

Comments
 (0)