pupoP
tRPC2y ago
4 replies
pupo

Missing content type header for mutations leads to 405

Im migrating my app from Next Pages Router to App Router. I implemented the new trpc wrapper as explained here and everything seems to be working as I expected, except mutations. Mutations fail on preflight with a 405 (local development, same server).

The only difference between the "before" and "after" of the migration (As well as the only difference to queries) is that mutations are missing a content-type: "json" (See attached). Any idea how I can fix this?
Screenshot_2024-02-29_19-07-19.png
Screenshot_2024-02-29_19-07-35.png
Solution
Ok .... so for prosterity the issue was in the server API route. it was missing a POST handler. Fixed version:


import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { appRouter } from "@cf/providers/trpc";

const handler = (req: Request) =>
  fetchRequestHandler({
    endpoint: "/api/trpc2",
    router: appRouter,
    req,
    createContext: async () => ({}),
  });

export const POST = handler;
export const GET = handler;
Was this page helpful?