http_request_body_json_decoder

package module
v0.0.0-...-fb0b8b3 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 5 Imported by: 0

README

HTTP request body JSON decoder

Table of Content

Examples

package main

import (
	"net/http"
	"time"

	"entrlcom.dev/http-request-body-json-decoder"
)

const maxBytes = 1 << (10 * 1) * 2 // 2 KiB.

type Request struct {
	DateOfBirth time.Time `json:"date_of_birth"`
	Email       string    `json:"email"`
	Name        string    `json:"name"`
	Password    string    `json:"password"`
}

func Handle(w http.ResponseWriter, r *http.Request) {
	var request Request

	// Decode HTTP request body to struct.
	if err := http_request_body_json_decoder.Decode(w, r, &request, maxBytes); err != nil {
		// TODO: Handle error.
		return
	}

	// ...
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInternal              = errors.New("internal error")
	ErrInvalidJSON           = errors.New("invalid json")
	ErrRequestEntityTooLarge = errors.New("request entity too large")
	ErrUnsupportedMediaType  = errors.New("unsupported media type")
)

Functions

func Decode

func Decode(
	w http.ResponseWriter,
	r *http.Request,
	v any,
	n int64,
) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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