I'm not sure how to describe the problem to its fullest, I'm already a week in playing with this and can't get it to work...
I have a FastAPI server running as Lambda connected to API Gateway
CORS is enabled in both FastAPI and API Gateway.
I have NextJS running on Amplify, the requests that NextJS is performing are working, but the requests on my client side do not and have a CORS error.
my API is api.example.com/api
while App is example.com
What Iv'e tried:
- Adding the following headers to each request
"Access-Control-Allow-Origin": '*', "Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version", "Access-Control-Allow-Methods": 'GET, POST, PUT, DELETE, OPTIONS'
- Adding re-writes to make sort of a proxy
- Using CORS chrome extension, still does not work
Signup route which is done by next and not the client works
yet request done by client
I've prob tried many more things in between, I'm just clueless
This is my code for creating Axios Instance (Removed the headers but it did not work with them also)
import axios from 'axios';
export const api = (session: any): any => {
const headers: { [key: string]: string | number } = {
"Accept": 'application/json',
}
if (session?.user) {
headers.Authorization = session.user.token;
}
const api = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_DOMAIN,
withCredentials: true,
headers
});
return api;
};
Any help is very very much appreciated
EDIT:
ERROR itself
The lambda does not seems to even get the requests that done by my client, only the ones from Nextjs backend.
It makes me think it's the API Gateway configuration or my client request headers configuration.
Also tried:
- Adding
Access-Control-Allow-Credentials
header as true, on client, and API gateway. - Added for my FastAPI CORS middleware-specific set of origins with my
https://example.com