### Install Core httpsuite Library Source: https://github.com/rluders/httpsuite/blob/main/README.md Command to install the core httpsuite library using Go modules. ```bash go get github.com/rluders/httpsuite/v3 ``` -------------------------------- ### Install Optional Validation Adapter Source: https://github.com/rluders/httpsuite/blob/main/README.md Command to install the optional validation adapter for httpsuite. ```bash go get github.com/rluders/httpsuite/validation/playground ``` -------------------------------- ### RFC 9457 Problem Details Example Source: https://github.com/rluders/httpsuite/blob/main/README.md Illustrates the structured RFC 9457 Problem Details format for API errors, providing consistency over ad-hoc responses. ```json { "type": "...", "title": "...", "status": 400, "detail": "..." } ``` -------------------------------- ### Curl Command for Core Request Parsing Source: https://github.com/rluders/httpsuite/blob/main/README.md Example cURL command to test the /users/{id} endpoint created with the core request parsing snippet. ```bash curl -X POST http://localhost:8080/users/123 \ -H "Content-Type: application/json" \ -d '{"name":"Ada"}' ``` -------------------------------- ### Global Validator Configuration Source: https://github.com/rluders/httpsuite/blob/main/README.md Configures a global validator for request parsing using a custom validator implementation. This setup enables automatic validation for all subsequent ParseRequest calls. ```go validator := playground.NewWithValidator(nil, &httpsuite.ProblemConfig{ BaseURL: "https://api.example.com", }) httpsuite.SetValidator(validator) req, err := httpsuite.ParseRequest[*CreateUserRequest]( w, r, chi.URLParam, &httpsuite.ParseOptions{ MaxBodyBytes: 1 << 20, }, "id", ) ``` -------------------------------- ### Simplified Request Handling With httpsuite Source: https://github.com/rluders/httpsuite/blob/main/README.md Demonstrates the concise and safer way to handle HTTP requests using httpsuite, consolidating parsing, binding, and response logic. ```go req, err := httpsuite.ParseRequest[*MyRequest](w, r, chi.URLParam, nil, "id") if err != nil { return } httpsuite.OK(w, req) ``` -------------------------------- ### Problem Builder and Response Source: https://github.com/rluders/httpsuite/blob/main/README.md Demonstrates building a custom problem response using a builder pattern. This allows for detailed configuration of problem types, titles, details, and instances before writing the response. ```go problem := httpsuite.Problem(http.StatusNotFound). Type(httpsuite.GetProblemTypeURL("not_found_error")), Title("User Not Found"). Detail("user 42 does not exist"). Instance("/users/42"). Build() httpsuite.RespondProblem(problem). Header("X-Trace-ID", traceID). Write(w) ``` -------------------------------- ### Direct Response Helpers Source: https://github.com/rluders/httpsuite/blob/main/README.md Provides direct helper functions for common HTTP responses like OK, Created, and Problem responses. These are useful for simple, straightforward response generation. ```go httpsuite.OK(w, user) httpsuite.OKWithMeta(w, users, httpsuite.NewPageMeta(page, pageSize, totalItems)) httpsuite.Created(w, user, "/users/42") httpsuite.ProblemResponse(w, httpsuite.NewNotFoundProblem("user not found")) ``` -------------------------------- ### Manual Request Handling Without httpsuite Source: https://github.com/rluders/httpsuite/blob/main/README.md Shows the traditional, more verbose approach to handling HTTP requests in Go without using a library like httpsuite. This often involves manual JSON decoding, path parameter parsing, and error handling. ```go var req MyRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, "invalid body", 400) return } idStr := chi.URLParam(r, "id") id, err := strconv.Atoi(idStr) if err != nil { http.Error(w, "invalid id", 400) return } // validation... // more error handling... ``` -------------------------------- ### Advanced Problem Response Builder Source: https://github.com/rluders/httpsuite/blob/main/README.md Demonstrates the advanced builder pattern for constructing custom RFC 9457 Problem responses. ```go httpsuite.Problem(...).Title(...).Build() ``` -------------------------------- ### Parse Request with Path Parameter Binding Source: https://github.com/rluders/httpsuite/blob/main/README.md Parses a request body, binds path parameters, and optionally performs validation. Use this for a consolidated request handling flow. ```go req, err := httpsuite.ParseRequest[*MyRequest](w, r, chi.URLParam, nil, "id") if err != nil { return } ``` -------------------------------- ### Simple Success Response Source: https://github.com/rluders/httpsuite/blob/main/README.md Uses a direct helper to send a simple success response with data. ```go httpsuite.OK(w, data) ``` -------------------------------- ### Fluent Success Response with Metadata Source: https://github.com/rluders/httpsuite/blob/main/README.md Uses a fluent API to construct a success response, including optional generic metadata. ```go httpsuite.Reply().Meta(meta).OK(w, data) ``` -------------------------------- ### Fluent Response Builders Source: https://github.com/rluders/httpsuite/blob/main/README.md Offers a fluent API for constructing HTTP responses, allowing chaining of methods for setting metadata, headers, and response bodies. This provides a more readable and composable way to build responses. ```go httpsuite.Reply(). Meta(httpsuite.NewPageMeta(page, pageSize, totalItems)). OK(w, users) httpsuite.Reply(). Header("X-Request-ID", requestID). Created(w, user, "/users/42") ``` -------------------------------- ### Core Request Parsing with Chi Source: https://github.com/rluders/httpsuite/blob/main/README.md Parses an incoming HTTP request into a strongly-typed Go struct using the 'chi' router. It demonstrates how to extract URL parameters and handle potential parsing errors. ```go package main import ( "net/http" "strconv" "github.com/go-chi/chi/v5" "github.com/rluders/httpsuite/v3" ) type CreateUserRequest struct { ID int `json:"id"` Name string `json:"name"` } func (r *CreateUserRequest) SetParam(fieldName, value string) error { if fieldName != "id" { return nil } id, err := strconv.Atoi(value) if err != nil { return err } r.ID = id return nil } func main() { router := chi.NewRouter() router.Post("/users/{id}", func(w http.ResponseWriter, r *http.Request) { req, err := httpsuite.ParseRequest[*CreateUserRequest](w, r, chi.URLParam, nil, "id") if err != nil { return } httpsuite.OK(w, req) }) _ = http.ListenAndServe(":8080", router) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.