#help endpoints cors

12 messages · Page 1 of 1 (latest)

fallow bluff
#

Hello, I need help, I have an application in Astro and another in Angular. The Astro application has some endpoints that I need to consume from Angular, but when making the requests I get the famous cors error. Is there an example of how to configure this in Astro?

plucky harbor
#

I made a function called headers:

export const getHeaders = (origin) => {
  if (
    origin === 'tauri://localhost' ||
    origin === 'https://tauri.localhost' ||
    origin === 'http://localhost.atavismxi.com:4321' ||
    origin === 'http://localhost:1420' ||
    origin === 'https://atavismxi.com'
  ) {
    return {
      'Access-Control-Allow-Origin': origin,
      'Content-Type': 'application/json',
      'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
      'Access-Control-Allow-Headers': 'Content-Type',
      'Access-Control-Allow-Credentials': 'true',
    }
  }
}

Then I import my getHeaders function in each endpoint: import { getHeaders } from '../../../utils/headers'

Pass origin to the headers function:

export async function GET({ cookies, request }) {
    const origin = request.headers.get('origin')
    const headers = getHeaders(origin)
}

Put headers in the response:

    return new Response(JSON.stringify(results), {
      status: 200,
      headers: headers,
    })

Maybe this is crazy?

fallow bluff
#

Hello, thank you for your answers. I tried both options and configurations and it still doesn't work. I share the configurations I made.

plucky harbor
#

I couldn't get CORs errors to go away unless I set the headers in each endpoint. If that makes sense. Like in each GET, POST,PUT etc. Not sure what the right terminology is

plucky harbor
plucky harbor
#

oh I just saw the screenshot for the endpoint

#

if the post is still giving you CORs errors maybe trying settings it's access-control-allow-origin to * for testing purposes

#

do you see any console logs like this pic where it shows you where the request origin is?

fallow bluff
#

In both development and production I get the same error. I have tried placing the specific path and also the *