Home > Software design >  heroku send post receives as options
heroku send post receives as options

Time:10-11

I have deployed an API on Heroku and when i send post request from my website the Heroku logs show method=options and the does not work. When I send from my local machine, Heroku logs show method=post and all works great.

I have found this post in StackOverflow Heroku use Options instead of POST method but that solution does not solve my issue.

Heroku logs, first is from local, second is from website:

2022-10-06T23:57:19.315699 00:00 heroku[router]: at=info method=POST path="/api/smsNotif/applySmsNotif" host=***** request_id=88da98c7-f136-4775-a52a-b10b104b2cff fwd="****" dyno=web.1 connect=0ms service=11ms status=200 bytes=462 protocol=https

2022-10-07T00:01:05.970299 00:00 heroku[router]: at=info method=OPTIONS path="/api/smsNotif/applySmsNotif" host=***** request_id=039a51c3-a976-47f7-bd03-4c52bc71361f fwd="****" dyno=web.1 connect=0ms service=1ms status=204 bytes=277 protocol=https

my request is from axios on a hosted website:

api({
        method: "POST",
        url: "/api/smsNotif/applySmsNotif",
        data: appData,
      });

API code to fix the method=options:

const app = express();

app.use(cors({ origin: ['https://forms.ccenttcs.com/', 'http://localhost:9002'], }))
app.use(express.json());
app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    // handle OPTIONS method
    if ('OPTIONS' == req.method) {
        return res.sendStatus(200);
    } else {
        next();
    }
});

CodePudding user response:

Handle option method work only without cors module. As you use cors module so you need to make following changes.

const app = express();

app.use(cors({ origin: ['https://forms.ccenttcs.com/', 'http://localhost:9002'], }))
app.options('*', cors()) // include before other routes

if you face CORS error then change cors config by removing origin and test.

app.use(cors())

CodePudding user response:

After a few reloads the original code worked, from(Heroku use Options instead of POST method)

Thank you @Dipten

One thing to watch for is in :

app.use(cors({ origin: ['https://forms.ccenttcs.com/', 'http://localhost:9002'], }))

I use my API for multiple apps and I need to have all of them listed for CORS in the code above.

const app = express();

app.use(cors({ origin: ['https://forms.ccenttcs.com/', 'http://localhost:9002'], }))
app.use(express.json());
app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    // handle OPTIONS method
    if ('OPTIONS' == req.method) {
        return res.sendStatus(200);
    } else {
        next();
    }
});
  • Related