-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(client): omit content type header in GET requests #6914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(client): omit content type header in GET requests #6914
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughImplements conditional omission of the Content-Type header for GET requests in HTTP link utilities and adds end-to-end tests verifying header behavior for GET, POST, and methodOverride scenarios. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant C as Client
participant HU as httpUtils
participant F as fetch
participant S as Server
C->>HU: makeRequest(opts)
Note over HU: method = opts.methodOverride ?? METHOD[opts.type]
alt method == GET
Note over HU: omit Content-Type header
else
Note over HU: set Content-Type: application/json (if provided)
end
HU->>F: fetch(url, { method, headers, body })
F->>S: HTTP request
S-->>F: Response
F-->>C: Result
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Assessment against linked issues
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
|
@anatolzak is attempting to deploy a commit to the trpc Team on Vercel. A member of the Team first needs to authorize it. |
@trpc/client
@trpc/next
@trpc/react-query
@trpc/server
@trpc/tanstack-react-query
@trpc/upgrade
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (4)
packages/client/src/links/internals/httpUtils.ts (1)
209-216: Optional: Skip empty-body Content-Type and normalize headers in fetchHTTPResponseVerified that the only explicit “content-type” header is set in fetchHTTPResponse (packages/client/src/links/internals/httpUtils.ts lines 209–216), and that all JSON requests (via jsonHttpRequester) and batch streams funnel through this function. Because getBody returns undefined for queries (and any mutation with input === undefined), we can safely skip setting
content-typewhenever the body is empty to avoid needless CORS preflights. Also, user-provided headers from resolvedHeaders are merged as-is, so normalizing keys to lowercase prevents duplicates likeContent-Typevscontent-type.Files to update:
- packages/client/src/links/internals/httpUtils.ts
Proposed diff at ~lines 205–215:
@@ export async function fetchHTTPResponse(opts: HTTPRequestOptions) { - const method = opts.methodOverride ?? METHOD[opts.type]; + const method = opts.methodOverride ?? METHOD[opts.type]; + const hasBody = body != null; @@ - const headers = { - ...(opts.contentTypeHeader && method !== 'GET' - ? { 'content-type': opts.contentTypeHeader } - : {}), - ...(opts.trpcAcceptHeader - ? { 'trpc-accept': opts.trpcAcceptHeader } - : undefined), - ...resolvedHeaders, - }; + const headers = { + // Only set Content-Type when not GET and there’s a body + ...(opts.contentTypeHeader && method !== 'GET' && hasBody + ? { 'content-type': opts.contentTypeHeader } + : {}), + // Always include tRPC accept header if provided + ...(opts.trpcAcceptHeader + ? { 'trpc-accept': opts.trpcAcceptHeader } + : {}), + // Normalize user headers to lowercase keys to avoid duplicates + ...Object.fromEntries( + Object.entries(resolvedHeaders).map(([k, v]) => [k.toLowerCase(), v]), + ), + };You may also consider guarding
trpc-acceptthe same way (e.g.&& method !== 'GET') if you want to avoid preflights when streaming GET batches.packages/tests/server/contentTypeHeaders.test.ts (3)
15-41: Strengthen assertions: also assert no body and that input is in the URL for GET.Both checks help catch regressions (e.g., accidentally sending a body or dropping URL-encoded input).
const [url, options] = mockFetch.mock.calls[0]!; expect(options.method).toBe('GET'); expect(options.headers).not.toHaveProperty('content-type'); + // No payload with GET + expect(options.body).toBeUndefined(); + // Input should be URL-encoded on GET + expect(String(url)).toContain('input='); await close();
43-69: Add body assertion to ensure POST carries JSON payload.Verifies we’re actually sending a JSON string when posting.
const [url, options] = mockFetch.mock.calls[0]!; expect(options.method).toBe('POST'); expect(options.headers).toHaveProperty('content-type', 'application/json'); + expect(typeof options.body).toBe('string'); + expect(options.body).toBe(JSON.stringify('test'));
71-105: Method override test: also assert no URL-encoded input and body presence.Confirms the query was truly POSTed (payload in body, not query string).
const [url, options] = mockFetch.mock.calls[0]!; expect(options.method).toBe('POST'); expect(options.headers).toHaveProperty('content-type', 'application/json'); + expect(String(url)).not.toContain('input='); + expect(options.body).toBe(JSON.stringify('test'));
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
packages/client/src/links/internals/httpUtils.ts(3 hunks)packages/tests/server/contentTypeHeaders.test.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/tests/server/contentTypeHeaders.test.ts (2)
packages/tests/server/___testHelpers.ts (1)
routerToServerAndClientNew(36-74)packages/client/src/links/httpLink.ts (1)
httpLink(75-141)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
- GitHub Check: E2E-tests (nuxt)
- GitHub Check: E2E-tests (minimal-react)
- GitHub Check: E2E-tests (fastify-server)
- GitHub Check: E2E-tests (express-server)
- GitHub Check: E2E-tests (cloudflare-workers)
- GitHub Check: E2E-tests (.test/ssg)
- GitHub Check: E2E-tests (.test/internal-types-export)
- GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 20.x)
- GitHub Check: e2e-legacy-node (next-prisma-todomvc, 20.x)
- GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 18.x)
- GitHub Check: e2e-legacy-node (next-prisma-todomvc, 18.x)
- GitHub Check: e2e-legacy-node (next-prisma-starter, 20.x)
- GitHub Check: e2e-legacy-node (next-prisma-starter, 18.x)
- GitHub Check: Release using pkg.pr.new
- GitHub Check: Test a monorepo using built declaration files
- GitHub Check: E2E-tests (Bun) (bun, ubuntu-latest)
- GitHub Check: E2E-tests (Deno) (deno-deploy)
- GitHub Check: test
- GitHub Check: Lint and auto-fix
🔇 Additional comments (3)
packages/client/src/links/internals/httpUtils.ts (2)
200-200: Correct precedence: single computed HTTP method (incl. methodOverride).Deriving the method once with
opts.methodOverride ?? METHOD[opts.type]is clean and avoids recomputing. This also makes downstream checks (like header gating) consistent.
219-223: Consistent method usage in fetch call.Using the computed
methodhere ensures consistency with header gating and URL/body construction logic.packages/tests/server/contentTypeHeaders.test.ts (1)
1-14: Test harness looks solid.Lightweight
mockFetchwrapper keeps assertions simple and doesn’t mutate global fetch. Good call.
|
Good idea @anatolzak! |
|
This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you. |
Closes #6913
🎯 Changes
Omit the content-type request header when sending a GET request. This prevents the GET request from triggering pre-flight requests that would potentially not occur otherwise.
✅ Checklist
Summary by CodeRabbit
Bug Fixes
Tests