API not able to receive POST Method from Airtable Script fetch-POST

Viewed 8

I am writing an Airtable script to post data from the record to my API however when I run the code in airtable and it hit my API it says the Method is OPTIONS and give 405 error.

The API works as I try to post data from postman.

The error in airtable says: "ypeError: Failed to fetch at main on line 27 This error might be related to Cross-Origin Resource Sharing (CORS), a security mechanism that prevents websites from making malicious requests to other sites."

Error in console is: "OPTIONS /api/webhook 405 Method Not Allowed "

Below is my airtable script:

let table = base.getTable('Onboarding');
let view = table.getView('Grid view');

let records = await view.selectRecordsAsync()
let record = records.records[0]

var data = {user: 
  {    
   "firstName": record.getCellValueAsString('firstName'),
   "LastName": record.getCellValueAsString('lastName'),
   "email": record.getCellValueAsString('email'),
   },
 }

console.log(JSON.stringify(data), "data")

let response = await fetch('https://1412-76-133-90-147.ngrok.io/api/webhook', {
  method: 'POST',
  body: JSON.stringify(data),
  headers: {
    "Content-Type": 'application/json',
    "Accept": "application/json",
  }
})

let respJSON = await response.json()

console.log("Response", respJSON.text())

await table.updateRecordAsync(record.id, {
  response: respJSON,
});

Thanks for helping!

0 Answers
Related