Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!