Repository navigation
Closed
Description
Hi,
I have a very simple app that I am testing:
import * as trpc from '@trpc/server'
import superjson from 'superjson'
import { z } from 'zod'
const appRouter = trpc
.router()
.transformer(superjson)
.mutation('hello', {
input: z.string(),
resolve({ input }) {
console.log(input)
return { input }
}
})
export type AppRouter = typeof appRouter
// http server
const { listen } = trpc.createHttpServer({
router: appRouter,
createContext: () => ({})
})
listen(5050)when I call this endpoint with:
=> http POST 'localhost:5050/hello' input=asdasd
HTTP/1.1 400 Bad Request
Connection: keep-alive
Content-Length: 1735
Content-Type: application/json
Date: Wed, 04 Aug 2021 07:41:14 GMT
Keep-Alive: timeout=5
{
"error": {
"json": {
"code": -32600,
"data": {
"code": "BAD_REQUEST",
"path": "hello",
"stack": "TRPCError: [\n {\n \"code\": \"invalid_type\",\n \"expected\": \"string\",\n \"received\": \"undefined\",\n \"path\": [],\n \"message\": \"Required\"\n }\n]\n at ProcedureWithInput.parseInput (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:46:13)\n at /home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:62:21\n at Array.<anonymous> (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:66:20)\n at ProcedureWithInput.call (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:71:36)\n at Router.call (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:295:22)\n at Object.mutation (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/router-efe6cf32.cjs.dev.js:309:21)\n at Object.callProcedure (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/callProcedure-dfe3505e.cjs.dev.js:57:19)\n at /home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/index-5eec7158.cjs.dev.js:239:44\n at Array.map (<anonymous>)\n at Object.requestHandler (/home/bobby/Projects/neurocode/ts-micro-template/node_modules/@trpc/server/dist/index-5eec7158.cjs.dev.js:235:45)"
},
"message": "[\n {\n \"code\": \"invalid_type\",\n \"expected\": \"string\",\n \"received\": \"undefined\",\n \"path\": [],\n \"message\": \"Required\"\n }\n]"
}
},
"id": null
}
If I remove the transfomer(superjson) from the server it works as expected:
=> http POST 'localhost:5050/hello' input=asdasd
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 62
Content-Type: application/json
Date: Wed, 04 Aug 2021 07:42:23 GMT
Keep-Alive: timeout=5
{
"id": null,
"result": {
"data": {
"input": "asdasd"
},
"type": "data"
}
}
Metadata
Metadata
Assignees
Labels
No labels
Activity
KATT commentedon Aug 4, 2021
It's because it expects a superjson-serialized object - try disabling superjson on your backend or send
?input=%7B%22json%22%3A%22asdasd%22%7DKATT commentedon Aug 4, 2021
I got that input payload from doing
copy(encodeURIComponent(JSON.stringify({json: "asdasd"})))in my inspector (the fmt that superjson expects)KATT commentedon Aug 4, 2021
Without superjson you would need to send
input=%22hello%22(copy(encodeURIComponent(JSON.stringify("hello"))))donchev7 commentedon Aug 4, 2021
Without superjson I am able to do
I am using HTTPie. And note that I am calling a mutation endpoint with POST and not a query with queryParameters
donchev7 commentedon Aug 4, 2021
But nevermind, superjson is the problem here you are right. Even on a passthorugh:
I can see:
github-actions commentedon Oct 5, 2022
This issue has been locked because it had no new activity for 14 days. If you are running into a similar issue, please create a new issue. Thank you.