Add examples/ with runnable usage demos for all major features
All checks were successful
CI / test (push) Successful in 31s
All checks were successful
CI / test (push) Successful in 31s
Six examples covering the full API surface: - basic-client: retry, timeout, logging, response size limit - form-request: form-encoded POST for OAuth/webhooks - load-balancing: weighted endpoints, circuit breaker, health checks - server-basic: routing, groups, JSON helpers, health, custom 404 - server-protected: CORS, rate limiting, body limits, timeouts - request-id-propagation: cross-service request ID forwarding Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
41
examples/form-request/main.go
Normal file
41
examples/form-request/main.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// Demonstrates form-encoded requests for OAuth token endpoints and similar APIs.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"git.codelab.vc/pkg/httpx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
client := httpx.New(
|
||||
httpx.WithBaseURL("https://httpbin.org"),
|
||||
)
|
||||
defer client.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Form-encoded POST, common for OAuth token endpoints.
|
||||
req, err := httpx.NewFormRequest(ctx, http.MethodPost, "/post", url.Values{
|
||||
"grant_type": {"client_credentials"},
|
||||
"client_id": {"my-app"},
|
||||
"client_secret": {"secret"},
|
||||
"scope": {"read write"},
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
resp, err := client.Do(ctx, req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer resp.Close()
|
||||
|
||||
body, _ := resp.String()
|
||||
fmt.Printf("Status: %d\nBody: %s\n", resp.StatusCode, body)
|
||||
}
|
||||
Reference in New Issue
Block a user