Documentation
¶
Index ¶
- Constants
- Variables
- func ApplyBodyFormat(contentType string, body []byte) (kind, pretty, jsonStr string, isJSON bool)
- func DetectBodyKind(contentType string, body []byte) string
- func EncodeJSON(data any) ([]byte, error)
- func IsJSON(s string) bool
- func IsJSONBytes(data []byte) bool
- func IsXML(s string) bool
- func IsXMLBytes(data []byte) bool
- func MakeDir(dir string) error
- func ParseJSON(text string) (map[string]any, error)
- func PrettyBody(body []byte, kind string) (string, error)
- func PrettyHTML(data []byte) (string, error)
- func PrettyJSON(data []byte) (string, error)
- func PrettyXML(data []byte) (string, error)
Constants ¶
const ( BodyKindJSON = "json" BodyKindXML = "xml" BodyKindHTML = "html" BodyKindText = "text" )
Body kind constants used by egress and the response UI.
const MaxPrettyBytes = 32 << 20
MaxPrettyBytes is the hard ceiling on formatting a body.
A formatted copy is a second full representation of the body, and producing it needs a working buffer on top of that: formatting an 86 MB JSON response cost ~200 MB in copies and buffers. Past this size the raw body is displayed as received, which also lets the viewer read the response's own buffer instead of copying it.
Variables ¶
var ErrBodyNotFormatted = errors.New("body left unformatted")
ErrBodyNotFormatted reports that a body was deliberately left unformatted — it is already laid out in lines, or too large to reformat. Callers should display the raw body.
Functions ¶
func ApplyBodyFormat ¶ added in v0.7.0
ApplyBodyFormat sets Pretty/BodyKind/IsJSON/JSON on a response-like target.
When the body is left unformatted (already line-broken, or over MaxPrettyBytes) pretty is returned empty so the caller displays the raw body without copying it. jsonStr is still filled for bodies within MaxPrettyBytes, since jsonpath extraction needs a string; past that it is left empty rather than holding another copy of a very large body.
func DetectBodyKind ¶ added in v0.7.0
DetectBodyKind chooses a display format from Content-Type, then body sniffing.
func EncodeJSON ¶ added in v0.4.6
func IsJSONBytes ¶ added in v0.7.0
IsJSONBytes reports whether data is valid JSON without copying it into a string or building a value tree.
func IsXMLBytes ¶ added in v0.7.0
IsXMLBytes reports whether data looks like well-formed XML (or HTML-as-XML) without copying it into a string.
func PrettyBody ¶ added in v0.7.0
PrettyBody returns indented text for known structured kinds; otherwise the raw body.
A body that is already laid out in lines, or larger than MaxPrettyBytes, returns ErrBodyNotFormatted with no text.
func PrettyHTML ¶ added in v0.7.0
PrettyHTML applies a lightweight indent to HTML-ish markup. Failures return raw text.
func PrettyJSON ¶ added in v0.4.6
PrettyJSON indents data for display without decoding it.
This shows the response as the server sent it: object keys keep their original order, numbers keep their original text, and escape sequences are left as-is. The previous implementation decoded into interface{} and marshalled back, which reordered keys alphabetically (Go map marshalling) and round-tripped every number through float64 — so an int64 id like 9007199254740993 displayed as ...992. It also cost many times the body size in live objects: a 10 MB response pushed the Go heap from 45 MB to 195 MB, and the process never gave that back.
The buffer is pre-sized so it does not grow by doubling: an undersized guess on a 20 MB result churns through tens of MB of intermediate arrays before it settles. Four-space indenting of compact JSON roughly doubles it, so budget for that up front — overshooting costs one oversized array, undershooting costs a full realloc and copy.
Types ¶
This section is empty.