Files
hi-server/internal/gozero/rest/httpx/httpx.go
T
2026-05-27 23:51:25 -07:00

28 lines
548 B
Go

package httpx
import (
"context"
"encoding/json"
"net/http"
)
func Parse(r *http.Request, v any) error {
if r.Body == nil {
return nil
}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(v); err != nil {
return err
}
return nil
}
func ErrorCtx(_ context.Context, w http.ResponseWriter, err error) {
http.Error(w, err.Error(), http.StatusBadRequest)
}
func OkJsonCtx(_ context.Context, w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(v)
}