Accept header JSON responses #1127
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Browsers should keep their HTML experience while API clients need machine-readable data, so automatic JSON/JSON Lines responses based on the Accept header let one SQL file serve both purposes. Use cases include calling a page from
fetch(..., { headers: { "Accept": "application/json" } }), runningcurl -H "Accept: application/x-ndjson" ...to stream rows, or letting another backend query the same endpoint without adding a dedicatedjsoncomponent.This change introduces a
ResponseFormatenum, honors the client’s Accept header when building the response, and keeps CSP headers limited to HTML output. The render context can now choose between HTML, JSON, or JSON Lines writers, and the JSON renderer can pre-seed its output so the first SQL row isn’t skipped.Documentation is updated via migration 73 to explain the Accept header behavior alongside the existing
jsoncomponent, and the new tests cover JSON arrays, JSON Lines, HTML fallbacks, redirects, and headers so we can maintain the dual-mode experience.Testing:
cargo test --test mod -- data_formats::test_accept_json_returns_json_array;cargo test --test mod -- data_formats::test_accept_ndjson_returns_jsonlines;cargo test --test mod -- data_formats::test_accept_html_returns_html;cargo test --test mod -- data_formats::test_accept_wildcard_returns_html;cargo test --test mod -- data_formats::test_accept_json_redirect_still_works;cargo test --test mod -- data_formats::test_accept_json_headers_still_work.