How to add Headers to tRPC route?

Does anyone know how I can pass custom headers when calling a tRPC route? I would like to append custom headers when calling one of my routes i.e import {api} from "~/utils/api"; const myRoute = api.myRouter.myFunc.... So whether its a get or a mutation i would like to append custom headers prior to making the call, does anyone know if this is possible?
10 Replies
Tom
Tom13mo ago
If it’s on the server side can you use middleware? If not why do you need headers?
gxp91
gxp9113mo ago
i need the client side to append their captcha v3 token so i can use the middleware to validate they are not bots. the easiest way to do this for every method is to add a custom X-reCaptcha-Token header
Tom
Tom13mo ago
Does it need to have a specific name? Or do you just need it to get to the server? If you don’t need a specific name I use nookies to get the cookies up to the server and then I stash it in the request context. If it needs to have a specific name I’d need to understand more about why
gxp91
gxp9113mo ago
it would need some sort of key so we can parse it out. The token when it hits server side the server makes an api call to google to validate its contents and get the score back.
Tom
Tom13mo ago
Yeah. Nookies basically lets you set whatever you want to a given string key. Then you can get it from the server I’m on my phone right now but this is how my app does auth
gxp91
gxp9113mo ago
ok ill do some googling on what in the world nookies are xD im familiar with cookies im assuming its something similar
Tom
Tom13mo ago
It’s just a cookie management library. There are a few it’s the one I use I use 1 function call to set a cookie on the client and another to get it in the server
gxp91
gxp9113mo ago
looks like i have to set up CORS to enable cookies so i will try and tackle that first
gxp91
gxp9113mo ago
Thanks @Tom3 saved me alot of time from looking into headers hahahah
Tom
Tom13mo ago
Np. Glad it helped
Want results from more Discord servers?
Add your server
More Posts