I am getting a 405 Method disallowed when trying to make POST requests on my production app. The POST request is working fine locally.
This is my api/backend/[...request]
:
import { getAccessToken } from '@auth0/nextjs-auth0';
import { NextRequest } from 'next/server';
const BACKEND_URL = process.env.NEXT_PUBLIC_BACKEND_URL;
async function prepareHeaders() {
const { accessToken } = await getAccessToken();
return {
access_token: accessToken || '',
};
}
async function prepareUrl(req: NextRequest) {
const path = req.url && req.url.split('/api/backend')[1];
return `${BACKEND_URL}${path}`;
}
export async function GET(req: NextRequest) {
const headers = await prepareHeaders();
const url = await prepareUrl(req);
return await fetch(url, {
method: 'GET',
headers: headers,
}).catch((e) => {
console.error(e);
return new Response('Internal Server Error', { status: 500 });
});
}
export async function POST(req: NextRequest, res: Response) {
const headers = await prepareHeaders();
const url = await prepareUrl(req);
let body = {};
try {
body = await req.json();
} catch (e) {
console.error(e);
return new Response('Invalid JSON', { status: 400 });
}
return await fetch(url, {
method: 'POST',
headers: { ...headers, 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
}