Unable to catch a request with method 'OPTIONS' in my proxy middleware

I'm using Django Rest Framework as backend API, in my nuxt 3 project I set up a proxy and it's working, except for requests with method "options". I use this method to retrieve the structure of the API from Django, my proxy is unable to catch the request and the client receives a 204 "no content" response from Nuxt, I think for CORS reasons, how can I pass the request to my proxy middleware?
No description
No description
2 Replies
Zampa
Zampa2y ago
you should be able to get the method and handle it accordingly via: const method = getMethod(event); https://www.jsdocs.io/package/h3#getMethod
jsDocs.io
h3@1.0.1 - jsDocs.io
Documentation for npm package h3@1.0.1 - jsDocs.io
Zampa
Zampa2y ago
Something like...
// Define the handler function
const handler = (event) => {
// Get the method of the event using the getMethod function
const method = getMethod(event);

// Check the value of the method variable using an if statement
if (method === 'OPTIONS') {
// If the method is 'OPTIONS', return an empty response with the appropriate headers
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*',
},
};
}

// If the method is not 'OPTIONS', return an error response
return {
statusCode: 405,
body: 'Method not allowed',
};
};
// Define the handler function
const handler = (event) => {
// Get the method of the event using the getMethod function
const method = getMethod(event);

// Check the value of the method variable using an if statement
if (method === 'OPTIONS') {
// If the method is 'OPTIONS', return an empty response with the appropriate headers
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*',
},
};
}

// If the method is not 'OPTIONS', return an error response
return {
statusCode: 405,
body: 'Method not allowed',
};
};