decodeJSONWithLimit is the size-configurable variant. Use this for endpoints that accept large payloads (e.g. bulk-import) where the default cap is too small — but always pass an explicit cap, never remove the wrapper.
(r *http.Request, v interface{}, maxBytes int64)
| 1979 | // default cap is too small — but always pass an explicit cap, never |
| 1980 | // remove the wrapper. |
| 1981 | func decodeJSONWithLimit(r *http.Request, v interface{}, maxBytes int64) error { |
| 1982 | // http.MaxBytesReader.Close() is a no-op; the decoder leaves r.Body at |
| 1983 | // EOF anyway. Setting this here also lets the server return a 413 |
| 1984 | // automatically via the error we wrap below. |
| 1985 | if r.Body != nil { |
| 1986 | r.Body = http.MaxBytesReader(nil, r.Body, maxBytes) |
| 1987 | } |
| 1988 | if err := json.NewDecoder(r.Body).Decode(v); err != nil { |
| 1989 | return fmt.Errorf("invalid JSON: %w", err) |
| 1990 | } |
| 1991 | return nil |
| 1992 | } |
| 1993 | |
| 1994 | // getWorkspaceID resolves workspace slug/ID from the request. |
| 1995 | // If RequireWorkspaceAccess already resolved the workspace, reads from context. |