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
If it’s on the server side can you use middleware? If not why do you need headers?
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
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
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.
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
ok ill do some googling on what in the world nookies are xD im familiar with cookies im assuming its something similar
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
looks like i have to set up CORS to enable cookies so i will try and tackle that first
Thanks @Tom3 saved me alot of time from looking into headers hahahah
Np. Glad it helped