Skip to content

Conversation

@lovasoa
Copy link
Collaborator

@lovasoa lovasoa commented Nov 24, 2025

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" } }), running curl -H "Accept: application/x-ndjson" ... to stream rows, or letting another backend query the same endpoint without adding a dedicated json component.

This change introduces a ResponseFormat enum, 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 json component, 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.

- Updated the JSON component description to clarify its integration with external services and the ability to serve both HTML and JSON based on the HTTP Accept header.
- Added examples demonstrating how to request JSON responses using `curl`.
- Removed the obsolete migration file that documented the JSON response format feature, consolidating information into the main documentation.
@lovasoa lovasoa merged commit 2af95a0 into main Nov 24, 2025
10 checks passed
@lovasoa lovasoa deleted the pages-as-json branch November 24, 2025 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants