Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
Express/axios don't set cookies and headers for response

I have very complicated problem. My project has 2 API's. 1 API is for front-end of my application and 1 is for back-end. How it works, is just I send request from front-end API to back-end API.

Front-end API has URL http://localhost:8080/api/ and back-end API has URL http://localhost:3000.

The problem is front-end API can't get cookies from back-end API.

Here is example, function of front-end API just send request to back-end:

router.get('/test-front-api', async (req, res) => {
  const data = await api.get('/test-back-api')
  return res.json(data.data)
})

Back-and just sends cookie and some random text:

router.get("/test-back-api", (req, res) => {
  res.cookie("test", "cookie")
  res.send("Hi from back-end")
})

A here is where I have problem:

router.get('/test-front-api', async (req, res) => {
  const data = await api.get('/test-back-api')
  console.log(data.headers) // If you do console.log here you will this cookie test
  return res.json(data.data)
})

But this cookie is not set in browser.

Let me show one more example. In browser I can just type http://localhost:8080/api/test-front-api and here is result, no cookie:

enter image description here

But, if I type not front-end API endpoint, but back-end, everything works perfect:

enter image description here

I was trying literally everything I found about cors options, axios {withCredentials: true} etc. Nothing works.

I have found one solution, but I don't like it:

router.get('/test-front-api', async (req, res) => {
  const data = await api.get('/test-back-api')
  // Something like this
  res.cookie("test", JSON.stringify(data.headers['set-cookie']))
  return res.json(data.data)
})

So, my question is why cookie is no set by front-end endpoint?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I have found not reason of the problem, but solution that seems to be ok. As I said, there are 2 API's - front-end and back-end.

From back-end API I get this cookie, but actually, it makes no sense to send it in header. Why? Because the front-end API will send it back on front.

So, using example above, you can do this, first, just send this cookies in body:

router.get("/test-back-api", (req, res) => {
  res.json({cookie: "cookie"})
  res.send("Hi from back-end")
})

And then, in front-end API, handle it:

router.get('/test-front-api', async (req, res) => {
  const data = await api.get('/test-back-api')
  res.cookie("test", data.cookie)
  return res.json(data.data)
})

But I still have no idea, why I can send the same headers twice, through back-end API, then front-end API and finally on front.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda