Next.js Discord

Discord Forum

My fetch POST request is not working in production and no logs

Unanswered
Short-tailed Shearwater posted this in #help-forum
Open in Discord
Avatar
Short-tailed ShearwaterOP
My fetch POST request is not executing. It does not throw any errors, and I am checking for result.ok and try catch block. It also does not execute anything past the fetch request. This error only occurs in production. I tried allowing everything in cors but it didn't work. I am using Vercel to deploy. All my environment variables are loaded in vercel

Check the code:
console.log('Calling fetch...')

  try {
    const resultado = await fetch(process.env.API_URL, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `ApiKey ${process.env.API_KEY}`,
      },
      method: 'POST',
      body: JSON.stringify({})
    })

    if (!resultado.ok) {
      console.log('Fetch error')
      console.log(await resultado.json())
      throw new Error('Fetch error')
    }

    console.log('Fetch OK')

    return await resultado.json()
  } catch (e) {
    console.log('Fetch error')
    console.log(e)
  }

6 Replies

Avatar
Tomistoma
Are you making the POST request from the client or from the server?
Avatar
Short-tailed ShearwaterOP
from the server
Avatar
Tomistoma
Got it. Have you checked if console.log(process.env.API_URL) and console.log(process.env.API_KEY) before the try-catch gives out a result?
Avatar
Short-tailed ShearwaterOP
hmm, good point, ill try
yeah they are, and I am using this API-KEY to make a GET request and it works
what pieces of code should I put here?