httpx

package
v0.0.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(c echo.Context, v any, pattern string) error

Parse handles the complete parsing of an HTTP request, processing path parameters, query parameters, form data, headers, and JSON body. It also validates the parsed data if a validator is configured.

Parameters:

  • c: The Echo context containing the HTTP request
  • v: A pointer to the struct where parsed data will be stored
  • pattern: The URL pattern for path parameter extraction

The target struct can use the following tags:

  • `path:"name"` or `path:"name,optional"` for path parameters
  • `query:"name"` or `query:"name,optional"` for query parameters
  • `form:"name"` or `form:"name,optional"` for form data
  • `file:"name"` or `file:"name,optional"` for file metadata
  • `header:"name"` or `header:"name,optional"` for headers

Returns an error if parsing fails or validation fails.

func ParseForm

func ParseForm(c echo.Context, v any) error

ParseForm handles both regular form data and multipart form data, including file uploads. Form values are mapped using the "form" struct tag, and file metadata using the "file" tag.

Parameters:

  • c: The Echo context containing the HTTP request
  • v: A pointer to the struct where form values will be stored

Supports both single values and slices. Fields can be marked as optional:

type FormData struct {
    Name   string   `form:"name"`            // required field
    Tags   []string `form:"tags,optional"`   // optional slice
    File   string   `file:"upload"`          // required file
    Size   int64    `file:"upload,optional"` // optional file metadata
}

Returns an error if parsing fails, a required field is missing, or type conversion fails.

func ParseHeader

func ParseHeader(headerValue string) map[string]string

ParseHeader parses a single header value that contains key-value pairs separated by semicolons and returns them as a map.

Parameter:

  • headerValue: Raw header string in the format "key1=value1;key2=value2"

Returns a map of the parsed key-value pairs. Example input: "token=abc123;expire=3600" Returns: map[string]string{"token": "abc123", "expire": "3600"}

func ParseHeaders

func ParseHeaders(c echo.Context, v any) error

ParseHeaders extracts and parses HTTP headers into the target struct. Headers are mapped using the "header" struct tag.

Parameters:

  • c: The Echo context containing the HTTP request
  • v: A pointer to the struct where header values will be stored

Header fields can be marked as optional using the "optional" tag modifier:

type Headers struct {
    Auth string `header:"Authorization"`           // required
    Track string `header:"X-Tracking,optional"`   // optional
}

Returns an error if a required header is missing or if type conversion fails.

func ParseJsonBody

func ParseJsonBody(c echo.Context, v any) error

ParseJsonBody decodes JSON data from the request body into the target struct. Only processes the request if Content-Type is application/json.

Parameters:

  • c: The Echo context containing the HTTP request
  • v: A pointer to the struct where JSON data will be decoded

The body size is limited to maxBodyLen (8MB) to prevent memory exhaustion. Returns an error if JSON decoding fails.

func ParsePath

func ParsePath(c echo.Context, v any, pattern string) error

ParsePath extracts and parses URL path parameters into the target struct. Path parameters are mapped using the "path" struct tag.

Parameters:

  • c: The Echo context containing the HTTP request
  • v: A pointer to the struct where path values will be stored
  • pattern: The URL pattern for parameter extraction

Supports both simple and embedded parameters. Fields can be marked as optional:

type PathParams struct {
    ID   string `path:"id"`            // required
    Slug string `path:"slug,optional"` // optional
}

Returns an error if parsing fails, a required parameter is missing, or type conversion fails.

func ParseQuery added in v0.0.4

func ParseQuery(c echo.Context, v any) error

ParseQuery extracts and parses URL query parameters into the target struct. Query parameters are mapped using the "query" struct tag.

Parameters:

  • c: The Echo context containing the HTTP request
  • v: A pointer to the struct where query values will be stored

Query fields can be marked as optional using the "optional" tag modifier:

type QueryParams struct {
    Page  int    `query:"page"`           // required
    Size  int    `query:"size,optional"`  // optional
    Sort  string `query:"sort,optional"`  // optional
}

Returns an error if a required parameter is missing or if type conversion fails.

func SetValidator

func SetValidator(val Validator)

SetValidator configures the validator used for validating parsed data. The validator is only called during the Parse function, not in individual parse functions.

Parameter:

  • val: Implementation of the Validator interface

func ValidateStruct

func ValidateStruct(v any) error

ValidateStruct validates the struct fields based on the `validate` tag.

Types

type Validator

type Validator interface {
	// Validate validates the request and parsed data.
	Validate(c echo.Context, data any) error
}

Validator defines the interface for validating the request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL