How to forward headers received from backend api call in server action?
Answered
New Guinea Freshwater Crocodile posted this in #help-forum
New Guinea Freshwater CrocodileOP
I have a backend api route which sets some cookies, this should happen on a server action tho. What im trying to do is, a fetch call will be made to backend and on success I have to forward the whole header or capture the set-cookie thing and set those cookies using cookies() function. How would I be able to manipulate headers in server action?
Answered by Jboncz
your .net backend can return the cookies, you would have to parse it from the 'const fetch' in the above example.
34 Replies
Question: Why are you calling yuor own backend via a server action? Why not just call the backend from the client side?
Seems like an uncessary hop.
New Guinea Freshwater CrocodileOP
i thought with app router everything had to go through nextjs server
'has to' is a strong word. lol
New Guinea Freshwater CrocodileOP
so its ok to do the backend calls directly from client side?
but in that case how would revalidations work?
Sure, why not. but to answer your original question proper you can manipulate the cookies in a server action using
https://nextjs.org/docs/app/api-reference/functions/cookies
https://nextjs.org/docs/app/api-reference/functions/cookies
I know cookies live in the headers, but you cant manipulate the headers in a server action, but you can manipulate the cookies...
New Guinea Freshwater CrocodileOP
hmm I thought we could also manipulate headers in server actions too
When you say manipulate, I need to understand what header you are trying to manipulate
the 'set-cookie' header is cookies()
and you can manipulate that
I dont fully understand what your trying to do unfortauntely.
New Guinea Freshwater CrocodileOP
mostly cookie xd but right now i have to parse the cookie values from backend and then set those with cookies() right?
'use server';
import { cookies } from 'next/headers'
async function dothings(data) {
const fetch = fetch('yadayadayada');
if (fetch == true) {
cookies().set('name','value')
}
return
}I still dont understand. When you execute a server action you are already on the server. Why call a endpoint? Why not just do it right there.
New Guinea Freshwater CrocodileOP
actually i have a seperate backend which is written in .net
Ah that makes more sense.
Then your on the right track.
your .net backend can return the cookies, you would have to parse it from the 'const fetch' in the above example.
Answer
New Guinea Freshwater CrocodileOP
would be great if i could skip the hop , but i think with app router its kinda antipattern maybe?
Yeah, dont skip the hop in this case.
I thought you were trying to access a backend api from next from a server action
but your hopping to another environment essentially, that is a ok
New Guinea Freshwater CrocodileOP
really wish we could just forward headers xd
Lol theres a good reason they dont let you manipulate the headers tho xD
New Guinea Freshwater CrocodileOP
have been using remix before and was just forwarding headers in actions
@Jboncz your .net backend can return the cookies, you would have to parse it from the 'const fetch' in the above example.
New Guinea Freshwater CrocodileOP
how do i mark this as answered?
New Guinea Freshwater CrocodileOP
ohh yeah now i found it xdd
thank you 🙂
Np bud.