PrismaP
Prisma2y ago
8 replies
Kev

GET request using JSON body

context: prisma,zod,typescript
I'm trying to run a
GET
request and passing in a json body, In order to make this work i need to convert it to using
.mutation
instead of
.query
. I was wondering if this is the standard for using prisma as I know posts will still return data.

for example: in my postman json:

{
    "user_id": 46216
}


 "message": "[\n  {\n    \"code\": \"invalid_type\",\n    \"expected\": \"object\",\n    \"received\": \"undefined\",\n    \"path\": [],\n    \"message\": \"Required\"\n  }\n]",

// This will fail as a GET request returning: ^
getFavoriteTruckStops: t.procedure
        .input(z.object({user_id: z.number().optional()}))
        .query(async ({input}) => {
            console.log('findUniqueFuelStop hit with input:', input);
            return prisma.truck_stop_favorites.findMany({
                where: {user_id: input.user_id}
            });
        })


//This works properly as POST and returns my data.
getFavoriteTruckStops: t.procedure
        .input(z.object({user_id: z.number().optional()}))
        .mutation(({input}) => {
            console.log('findUniqueFuelStop hit with input:', input);
            return prisma.truck_stop_favorites.findMany({
                where: {user_id: input.user_id}
            });
        })
Was this page helpful?