package httpximport ()// Header sets a header on a request or client.// @group Request Composition//// Applies to both client defaults and request-time headers.// Example: apply a header//// // Apply to all requests// c := httpx.New(httpx.Header("X-Trace", "1"))// httpx.Get[string](c, "https://example.com")//// // Apply to a single request// httpx.Get[string](httpx.Default(), "https://example.com", httpx.Header("X-Trace", "1"))func (, string) OptionBuilder {returnOptionBuilder{}.Header(, )}func ( OptionBuilder) (, string) OptionBuilder {return .add(bothOption(func( *Client) { .req.SetCommonHeader(, ) },func( *req.Request) { .SetHeader(, ) }, ))}// Headers sets multiple headers on a request or client.// @group Request Composition//// Applies to both client defaults and request-time headers.// Example: apply headers//// // Apply to all requests// c := httpx.New(httpx.Headers(map[string]string{// "X-Trace": "1",// "Accept": "application/json",// }))// httpx.Get[string](c, "https://example.com")//// // Apply to a single request// httpx.Get[string](httpx.Default(), "https://example.com", httpx.Headers(map[string]string{// "X-Trace": "1",// "Accept": "application/json",// }))func ( map[string]string) OptionBuilder {returnOptionBuilder{}.Headers()}func ( OptionBuilder) ( map[string]string) OptionBuilder {return .add(bothOption(func( *Client) { .req.SetCommonHeaders() },func( *req.Request) { .SetHeaders() }, ))}// UserAgent sets the User-Agent header on a request or client.// @group Request Composition//// Applies to both client defaults and request-time headers.// Example: set a User-Agent//// // Apply to all requests// c := httpx.New(httpx.UserAgent("my-app/1.0"))// httpx.Get[string](c, "https://example.com")//// // Apply to a single request// httpx.Get[string](httpx.Default(), "https://example.com", httpx.UserAgent("my-app/1.0"))func ( string) OptionBuilder {returnOptionBuilder{}.UserAgent()}func ( OptionBuilder) ( string) OptionBuilder {return .add(bothOption(func( *Client) { .req.SetUserAgent() },func( *req.Request) { .SetHeader("User-Agent", ) }, ))}// Query adds query parameters as key/value pairs.// @group Request Composition//// Applies to individual requests only.// Example: add query params//// c := httpx.New()// _ = httpx.Get[string](c, "https://example.com/search", httpx.Query("q", "go", "ok", "1"))func ( ...string) OptionBuilder {returnOptionBuilder{}.Query(...)}func ( OptionBuilder) ( ...string) OptionBuilder {return .add(requestOnly(func( *req.Request) {iflen()%2 != 0 {panic("httpx: Query expects even number of key/value arguments") }for := 0; < len(); += 2 { .AddQueryParam([], [+1]) } }))}// Queries adds multiple query parameters.// @group Request Composition//// Applies to individual requests only.// Example: add query params//// c := httpx.New()// _ = httpx.Get[string](c, "https://example.com/search", httpx.Queries(map[string]string{// "q": "go",// "ok": "1",// }))func ( map[string]string) OptionBuilder {returnOptionBuilder{}.Queries()}func ( OptionBuilder) ( map[string]string) OptionBuilder {return .add(requestOnly(func( *req.Request) { .SetQueryParams() }))}// Path sets a path parameter by name.// @group Request Composition//// Applies to individual requests only.// Example: path parameter//// type User struct {// Name string `json:"name"`// }//// c := httpx.New()// _ = httpx.Get[User](c, "https://example.com/users/{id}", httpx.Path("id", 42))func ( string, any) OptionBuilder {returnOptionBuilder{}.Path(, )}func ( OptionBuilder) ( string, any) OptionBuilder {return .add(requestOnly(func( *req.Request) { .SetPathParam(, fmt.Sprint()) }))}// Paths sets multiple path parameters.// @group Request Composition//// Applies to individual requests only.// Example: multiple path parameters//// type User struct {// Name string `json:"name"`// }//// c := httpx.New()// _ = httpx.Get[User](c, "https://example.com/orgs/{org}/users/{id}", httpx.Paths(map[string]any{// "org": "goforj",// "id": 42,// }))func ( map[string]any) OptionBuilder {returnOptionBuilder{}.Paths()}func ( OptionBuilder) ( map[string]any) OptionBuilder {return .add(requestOnly(func( *req.Request) { := make(map[string]string, len())for , := range { [] = fmt.Sprint() } .SetPathParams() }))}// Body sets the request body and infers JSON for structs and maps.// @group Request Composition//// Applies to individual requests only.// Example: send JSON body with inference//// type Payload struct {// Name string `json:"name"`// }//// c := httpx.New()// _ = httpx.Post[any, string](c, "https://example.com", nil, httpx.Body(Payload{Name: "Ana"}))func ( any) OptionBuilder {returnOptionBuilder{}.Body()}func ( OptionBuilder) ( any) OptionBuilder {return .add(requestOnly(func( *req.Request) {setBody(, ) }))}// JSON sets the request body as JSON.// @group Request Composition//// Applies to individual requests only.// Example: force JSON body//// type Payload struct {// Name string `json:"name"`// }//// c := httpx.New()// _ = httpx.Post[any, string](c, "https://example.com", nil, httpx.JSON(Payload{Name: "Ana"}))func ( any) OptionBuilder {returnOptionBuilder{}.JSON()}func ( OptionBuilder) ( any) OptionBuilder {return .add(requestOnly(func( *req.Request) { .SetBodyJsonMarshal() }))}// Form sets form data for the request.// @group Request Composition//// Applies to individual requests only.// Example: submit a form//// c := httpx.New()// _ = httpx.Post[any, string](c, "https://example.com", nil, httpx.Form(map[string]string{// "name": "Ana",// }))func ( map[string]string) OptionBuilder {returnOptionBuilder{}.Form()}func ( OptionBuilder) ( map[string]string) OptionBuilder {return .add(requestOnly(func( *req.Request) { .SetFormData() }))}// Timeout sets a per-request timeout using context cancellation.// @group Request Control//// Applies to both client defaults (via WithTimeout) and individual requests.// Example: timeout//// // Apply to all requests// c := httpx.New(httpx.Timeout(2 * time.Second))// httpx.Get[string](c, "https://example.com")//// // Apply to a single request// httpx.Get[string](httpx.Default(), "https://example.com", httpx.Timeout(2*time.Second))func ( time.Duration) OptionBuilder {returnOptionBuilder{}.Timeout()}func ( OptionBuilder) ( time.Duration) OptionBuilder {return .add(bothOption(func( *Client) { .req.SetTimeout() },func( *req.Request) { := .Context() , := context.WithTimeout(, ) .SetContext() .OnAfterResponse(func( *req.Client, *req.Response) error { ()returnnil }) }, ))}// Before runs a hook before the request is sent.// @group Request Control//// Applies to individual requests only.// Example: mutate req.Request//// c := httpx.New()// _ = httpx.Get[string](c, "https://example.com", httpx.Before(func(r *req.Request) {// r.EnableDump()// }))func ( func(*req.Request)) OptionBuilder {returnOptionBuilder{}.Before()}func ( OptionBuilder) ( func(*req.Request)) OptionBuilder {if == nil {return }return .add(requestOnly(func( *req.Request) { () }))}func setBody( *req.Request, any) {switch .(type) {casenil:returncasestring, []byte, io.Reader: .SetBody()default: .SetBodyJsonMarshal() }}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.