Documentation
¶
Overview ¶
Package jsonxmltool provides HTTP-oriented helpers for JSON, XML, uploads, and small web utilities.
Index ¶
- type JSONEnvelope
- type Kit
- func (k *Kit) EnsureDir(path string) error
- func (k *Kit) ErrorJSON(w http.ResponseWriter, err error, status ...int) error
- func (k *Kit) ErrorXML(w http.ResponseWriter, err error, status ...int) error
- func (k *Kit) PostJSON(uri string, data any, client ...*http.Client) (*http.Response, int, error)
- func (k *Kit) RandomString(n int) string
- func (k *Kit) ReadJSON(w http.ResponseWriter, r *http.Request, data any) error
- func (k *Kit) ReadXML(w http.ResponseWriter, r *http.Request, data any) error
- func (k *Kit) ReceiveOneUpload(r *http.Request, uploadDir string, rename ...bool) (*StoredFile, error)
- func (k *Kit) ReceiveUploads(r *http.Request, uploadDir string, rename ...bool) ([]*StoredFile, error)
- func (k *Kit) ServeAttachment(w http.ResponseWriter, r *http.Request, dir, name, displayName string)
- func (k *Kit) URLSlug(s string) (string, error)
- func (k *Kit) WriteJSON(w http.ResponseWriter, status int, data any, headers ...http.Header) error
- func (k *Kit) WriteXML(w http.ResponseWriter, status int, data any, headers ...http.Header) error
- type StoredFile
- type XMLEnvelope
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSONEnvelope ¶
type JSONEnvelope struct {
Error bool `json:"error"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
JSONEnvelope is a conventional JSON API wrapper with optional data.
type Kit ¶
type Kit struct {
MaxJSONSize int // cap on JSON request bodies; zero uses defaultBodyLimit
MaxXMLSize int // cap on XML request bodies; zero uses defaultBodyLimit
MaxFileSize int // cap per uploaded file in bytes; zero uses defaultBodyLimit
AllowedFileTypes []string // MIME types allowed for uploads (e.g. image/png); empty allows any detected type
AllowUnknownFields bool // when false, json.Decoder rejects keys not present on the target struct
ErrorLog *log.Logger // diagnostics for failures
InfoLog *log.Logger // routine operational messages
}
Kit groups configurable limits and loggers for the helper methods below. Construct one with NewKit and call methods on a pointer when you need to mutate limits in place.
func (*Kit) PostJSON ¶
PostJSON encodes data with json.Encoder into the POST body. An optional client overrides http.DefaultClient.
func (*Kit) RandomString ¶
RandomString returns a string of length n drawn from tokenAlphabet.
func (*Kit) ReadJSON ¶
ReadJSON decodes a single JSON value from r.Body into data (must be a pointer). When Content-Type is set, it must be application/json (case-insensitive).
func (*Kit) ReadXML ¶
ReadXML decodes a single XML document from r.Body into data (must be a pointer).
func (*Kit) ReceiveOneUpload ¶
func (k *Kit) ReceiveOneUpload(r *http.Request, uploadDir string, rename ...bool) (*StoredFile, error)
ReceiveOneUpload saves the first file in a multipart request to uploadDir.
func (*Kit) ReceiveUploads ¶
func (k *Kit) ReceiveUploads(r *http.Request, uploadDir string, rename ...bool) ([]*StoredFile, error)
ReceiveUploads stores multipart files under uploadDir, optionally replacing names with random tokens.
func (*Kit) ServeAttachment ¶
func (k *Kit) ServeAttachment(w http.ResponseWriter, r *http.Request, dir, name, displayName string)
ServeAttachment streams a file from dir/name as a download using displayName in Content-Disposition.
func (*Kit) URLSlug ¶
URLSlug lowercases s, replaces non-alphanumeric runs with hyphens, and trims edges.
type StoredFile ¶
StoredFile records how an upload was persisted on disk.
type XMLEnvelope ¶
type XMLEnvelope struct {
Error bool `xml:"error"`
Message string `xml:"message"`
Data any `xml:"data,omitempty"`
}
XMLEnvelope mirrors JSONEnvelope for XML responses.