In Swagger UI, when I try to execute GET on the endpoint I see
Cross-Origin Resource Sharing Error PreFlightMissingOriginHeader
The call reaches the API Endpoint (backed by Lambda) and the Lambda returns the correct response but its not displayed in the Swagger UI.
Below is the response returned from Lambda
{'statusCode': 200, 'body': '{"trace_id": "1703277823", "id": {"tenants": [{"tenant_name": "tenant1", "tenant_details": {"account_number": "1234"}}]}, "status": "success"}', 'headers': {'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': True, 'Access-Control-Allow-Methods': 'GET', 'Access-Control-Allow-Headers': ('Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization', 'authorization', 'sessionId')}}
I have used app.use(cors()) and app.options('*', cors())but it did not help.
Below is the code for node.js
const express = require('express')
const serverless = require('serverless-http')
const swaggerUI = require('swagger-ui-express')
const cors = require('cors');
let AWS = require('aws-sdk');
let s3 = new AWS.S3({
signatureVersion: 'v4',
});
const app = express()
module.exports.handler = async (event, context) => {
const signedUrl = s3.getSignedUrl(
'getObject',
{
Bucket: process.env.BUCKET_NAME,
Key: process.env.KEY_NAME,
Expires: 30
}
);
let options = {
swaggerOptions: {
url: signedUrl
}
}
app.options('*', cors())
app.use(cors())
// app.use('/swagger', swaggerUI.serve, swaggerUI.setup(null, options))
app.use('/swagger', cors(), function (req, res, next) {
console.log(req)
console.log(res)
next()
},swaggerUI.serve, swaggerUI.setup(null, options))
const handler = serverless(app);
return await handler(event, context)
}