b6d0c89aa1
Co-authored-by: multica-agent <github@multica.ai>
28 lines
548 B
Go
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)
|
|
}
|