{"id":32039860,"url":"https://github.com/emre-onal/http2struct","last_synced_at":"2026-07-12T01:31:09.483Z","repository":{"id":293627004,"uuid":"984637316","full_name":"emre-onal/http2struct","owner":"emre-onal","description":"Go Library for Converting / Binding HTTP Requests to Structs","archived":false,"fork":false,"pushed_at":"2025-10-18T20:29:01.000Z","size":26,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-29T07:47:34.443Z","etag":null,"topics":["binding","go","http","http2struct"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/emre-onal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-16T08:57:29.000Z","updated_at":"2025-10-18T20:28:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"e28aa39d-c62a-400b-b76d-8ce9dfdeda59","html_url":"https://github.com/emre-onal/http2struct","commit_stats":null,"previous_names":["nemre/http2struct","emre-onal/http2struct"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/emre-onal/http2struct","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emre-onal%2Fhttp2struct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emre-onal%2Fhttp2struct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emre-onal%2Fhttp2struct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emre-onal%2Fhttp2struct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emre-onal","download_url":"https://codeload.github.com/emre-onal/http2struct/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emre-onal%2Fhttp2struct/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35379590,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-11T02:00:05.354Z","response_time":104,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binding","go","http","http2struct"],"created_at":"2025-10-17T12:51:44.077Z","updated_at":"2026-07-12T01:31:09.471Z","avatar_url":"https://github.com/emre-onal.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# http2struct: Go Library for Converting / Binding HTTP Requests to Structs\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/emre-onal/http2struct.svg)](https://pkg.go.dev/github.com/emre-onal/http2struct)\n[![Go Report Card](https://goreportcard.com/badge/github.com/emre-onal/http2struct)](https://goreportcard.com/report/github.com/emre-onal/http2struct)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![GitHub release](https://img.shields.io/github/release/emre-onal/http2struct.svg)](https://github.com/emre-onal/http2struct/releases)\n\n`http2struct` is a lightweight, zero-dependency Go library that simplifies HTTP request processing by allowing you to easily transfer data from HTTP requests directly into Go structs. The library handles data from multiple sources including headers, URL query parameters, path parameters, form data, file uploads, and JSON body.\n\nThis streamlined approach to request binding eliminates boilerplate code and helps you write more readable, maintainable, and error-resistant applications.\n\n## Features\n\n- **Zero Dependencies:** Built using only Go's standard library\n- **Single Function API:** Converts an HTTP request to a struct with a single function call\n- **Comprehensive Source Support:** \n  - JSON body data (`json` tag)\n  - Form data (`form` tag)\n  - URL query parameters (`query` tag)\n  - Path parameters (`path` tag)\n  - HTTP headers (`header` tag)\n  - File uploads - both multipart form (`file` tag) and binary (`file:\"binary\"` tag)\n- **Automatic Type Conversion:** Handles conversion to various Go types:\n  - Boolean: `bool`\n  - Integers: `int`, `int8`, `int16`, `int32`, `int64`\n  - Unsigned integers: `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `uintptr`\n  - Floating point: `float32`, `float64`\n  - Complex numbers: `complex64`, `complex128`\n  - Strings: `string`\n  - Slices of the above types (comma-separated values are automatically split)\n- **File Upload Handling:** Manages both multipart form files and binary file uploads with the built-in `File` struct\n- **Extensive Error Reporting:** Provides detailed error messages for debugging\n- **Smart Data Binding:** Unlike some other binders, only binds fields with data present in the request, preventing invisible problems\n\n## Benefits\n\n| Benefits                | Before (using standard net/http)            | After (using emre-onal/http2struct)                                                                |\n| ----------------------- | ------------------------------------------ | ---------------------------------------------------------------------------------------------- |\n| ⌛️ Developer Time      | 😫 Expensive (too much parsing code)        | 🚀 **Faster** (define the struct and leave parsing to http2struct)                             |\n| ♻️ Code Repetition     | 😞 High                                    | 😍 **Lower** (eliminates repetitive request parsing code)                                      |\n| 📖 Code Readability     | 😟 Poor                                    | 🤩 **Highly readable** (declarative approach with struct tags)                                 |\n| 🔨 Maintainability      | 😡 Poor                                    | 🥰 **Highly maintainable** (centralized request handling logic)                                |\n| 🐞 Error Handling       | 😖 Manual for each field                   | 😎 **Comprehensive** (detailed errors for debugging)                                           |\n| 🔄 Type Safety          | 😨 Manual type conversion                  | 😌 **Automatic** (type-safe conversions with validation)                                       |\n\n## Installation\n\n```bash\ngo get -u github.com/emre-onal/http2struct\n```\n\n## Basic Usage\n\nDefine a struct with appropriate tags and convert your request:\n\n```go\ntype UserRequest struct {\n    Name      string   `json:\"name\"`           // From JSON body\n    Age       int      `json:\"age\"`            // From JSON body\n    Token     string   `header:\"Authorization\"` // From request header\n    Page      int      `query:\"page\"`          // From URL query parameter\n    UserID    uint64   `path:\"user_id\"`        // From path parameter\n    Nickname  string   `form:\"nickname\"`       // From form data\n    Tags      []string `query:\"tags\"`          // Handles comma-separated values\n    Avatar    File     `file:\"avatar\"`         // File upload from multipart form\n}\n\nfunc handler(w http.ResponseWriter, r *http.Request) {\n    var req UserRequest\n    \n    if err := http2struct.Convert(r, \u0026req); err != nil {\n        http.Error(w, err.Error(), http.StatusBadRequest)\n        return\n    }\n    \n    // Now req is populated with data from the request\n    fmt.Fprintf(w, \"Hello, %s!\", req.Name)\n}\n```\n\n## Advanced Usage\n\n### File Uploads\n\n`http2struct` provides a built-in `File` struct for handling file uploads:\n\n```go\ntype File struct {\n    Name    string // Original filename\n    Size    int64  // File size in bytes\n    Content []byte // File content\n}\n```\n\n#### Multipart Form File Uploads\n\n```go\ntype UploadRequest struct {\n    // As a value\n    Avatar File `file:\"avatar\"`\n    \n    // Or as a pointer\n    Document *File `file:\"document\"`\n}\n```\n\n#### Binary File Upload (Entire Request Body)\n\n```go\ntype BinaryUploadRequest struct {\n    // Use the special \"binary\" tag value\n    File File `file:\"binary\"`\n    \n    // Additional metadata can come from headers\n    ContentType string `header:\"Content-Type\"`\n    Filename    string `header:\"X-Filename\"`\n}\n```\n\n### Handling Multiple Data Sources\n\n`http2struct` allows you to combine data from multiple sources in a single request:\n\n```go\ntype ComplexRequest struct {\n    // User data from JSON body\n    User struct {\n        Name  string `json:\"name\"`\n        Email string `json:\"email\"`\n    } `json:\"user\"`\n    \n    // Configuration from query parameters\n    Page  int  `query:\"page\"`\n    Limit int  `query:\"limit\"`\n    \n    // Authentication from headers\n    Token string `header:\"Authorization\"`\n    \n    // Resource identifier from path\n    ID uint64 `path:\"id\"`\n    \n    // File uploads\n    Avatar    File  `file:\"avatar\"`\n}\n```\n\n## Error Handling\n\nThe `Convert` function returns detailed errors to help diagnose issues:\n\n```go\nerr := http2struct.Convert(r, \u0026req)\nif err != nil {\n    // Handle the error\n    log.Printf(\"Request conversion error: %v\", err)\n    http.Error(w, \"Bad request format\", http.StatusBadRequest)\n    return\n}\n```\n\nError messages are descriptive, indicating:\n- Invalid destination types\n- Field conversion failures\n- Unsupported types\n- Form parsing errors\n- JSON decoding issues\n\n## Best Practices\n\n- **Validate Input Data**: While `http2struct` handles conversion, you should still validate the business logic of the data\n- **Use Appropriate Types**: Choose struct field types that match the expected data to avoid conversion errors\n- **Consider Performance**: For large file uploads, process files directly rather than loading them all into memory\n- **Set Default Values**: Initialize struct fields with default values before conversion for optional parameters\n\n## Comparison with Other Libraries\n\n| Feature                 | http2struct             | gorilla/schema         | gin binding           |\n|-------------------------|-------------------------|------------------------|----------------------|\n| Zero Dependencies       | ✅                      | ❌                     | ❌                   |\n| JSON Body Support       | ✅                      | ❌                     | ✅                   |\n| Form Data Support       | ✅                      | ✅                     | ✅                   |\n| Query Parameter Support | ✅                      | ✅                     | ✅                   |\n| Path Parameter Support  | ✅                      | ❌                     | ❌                   |\n| Header Support          | ✅                      | ❌                     | ❌                   |\n| File Upload Support     | ✅                      | ❌                     | ✅                   |\n| Binary File Support     | ✅                      | ❌                     | ❌                   |\n| Detailed Error Messages | ✅                      | ❌                     | ✅                   |\n\n## Contributing\n\nContributions to improve `http2struct` are welcome! Here's how you can contribute:\n\n1. Fork the repository\n2. Create your feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'feat: add some amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.\n\n## FAQ\n\n### Q: Can I use http2struct with other web frameworks?\n**A:** Yes, the library works with any framework that uses the standard `net/http.Request` object, including Gin, Echo, Chi, etc.\n\n### Q: How does http2struct handle arrays or slices of values?\n**A:** For query parameters, path parameters, headers, and form values, comma-separated strings are automatically split and converted to slices of the appropriate type.\n\n### Q: What happens if a field can't be converted to the target type?\n**A:** The library will return a detailed error explaining which field failed conversion and why.\n\n### Q: Can I use nested structs?\n**A:** Yes, JSON body data can be mapped to nested structs. Other sources (query, path, header, form) work with flat structures.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femre-onal%2Fhttp2struct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femre-onal%2Fhttp2struct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femre-onal%2Fhttp2struct/lists"}