docD
tRPC3y ago
2 replies
doc

trpc + fastify POST request not working

I have a trpc mutation like this:

create: publicProcedure
    .input(z.object({ title: z.string() }))
    .mutation(({ input, ctx }) => {
      const id = db.posts.length + 1;
      const post = { id, title: input.title };
      db.posts.push(post);
      return post;
    }),


I am trying to call it via HTTP:

curl --request POST \
  --url http://localhost:2022/trpc/posts.create \
  --header 'Content-Type: application/json' \
  --data '{
    "title": "hello"
}'


it errors with:

"code": -32600,
"data": {
"code": "BAD_REQUEST",
"httpStatus": 400,
.....}

Not sure whats going on or why is it erroring. Any help is appreciated 🙂

"dependencies": {
    "@fastify/websocket": "^7.1.2",
    "@trpc/client": "^10.33.0",
    "@trpc/server": "^10.33.0",
    "fastify": "^4.13.0",
    "node-fetch": "^3.3.1",
    "superjson": "^1.12.4",
    "tslib": "^2.5.0",
    "ws": "^8.0.0",
    "zod": "^3.0.0"
  },
Was this page helpful?