Setting cookies in API Route Handlers isn't working at all
Answered
Thuan posted this in #help-forum
ThuanOP
How to set Cookies in Route Handlers?
I tried to follow the documentation but it doesn't work?
Please!
I tried to follow the documentation but it doesn't work?
Please!
Answered by Toyger
as both of us already mentioned above, you are running fetch inside route handler, it means that it is server is the one who are sending request, so it receive result.
So if you want user get result, you need to run this fetch from client side, not from route handler.
So if you want user get result, you need to run this fetch from client side, not from route handler.
5 Replies
Toyger
where are you calling this? because it doesn't look good, if you call it from user side then you don't need any env variable and just use
fetch('/api/cookie')@Toyger where are you calling this? because it doesn't look good, if you call it from user side then you don't need any env variable and just use `fetch('/api/cookie')`
He is using cookies() so he should be fetching on server and the response header go to server not browser
@Toyger where are you calling this? because it doesn't look good, if you call it from user side then you don't need any env variable and just use `fetch('/api/cookie')`
ThuanOP
fetch('/api/cookie') doesn't work for me, It only works when I add process.env.NEXT_PUBLIC_HOST@Thuan `fetch('/api/cookie')` doesn't work for me, It only works when I add `process.env.NEXT_PUBLIC_HOST`
Toyger
as both of us already mentioned above, you are running fetch inside route handler, it means that it is server is the one who are sending request, so it receive result.
So if you want user get result, you need to run this fetch from client side, not from route handler.
So if you want user get result, you need to run this fetch from client side, not from route handler.
Answer
@Toyger as both of us already mentioned above, you are running fetch inside route handler, it means that it is server is the one who are sending request, so it receive result.
So if you want user get result, you need to run this fetch from client side, not from route handler.
ThuanOP
Thank you so much, I got it