P
Prisma•4mo ago
Kevbot

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
}
{
"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}
});
})
"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}
});
})
//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}
});
})
5 Replies
Yetzederixx
Yetzederixx•4mo ago
by convenion GET requests aren't supposed to have bodies and some libraries will not support them I personally use PATCH requests for this anti-pattern since I use PUTs for all updates
Kevbot
Kevbot•4mo ago
Oh cool! does PATCH allow you to use .query then or do you continue using .mutation? ( i guess i can try it out ) 🙂
Yetzederixx
Yetzederixx•4mo ago
I don't know for sure, the project I'm using prisma for hasn't needed it but I don't think your actual problem here is with prisma, but express or whatever you are using
Kevbot
Kevbot•4mo ago
yeah thinking about it.. it looks like its more of a TRPC thing or general HTTP question. I appreciate your for hopping on!
Yetzederixx
Yetzederixx•4mo ago
The reason I went this route is if it works with POST but not GET it's probably the api layer why the original http standard left out bodies on get requests is beyond me, but then again rest api's weren't a thing in the 60's either
Want results from more Discord servers?
Add your server