Help

The Community will be temporarily unavailable starting on Friday February 28. We’ll be back as soon as we can! To learn more, check out our Announcements blog post.

getting 404 error using airtable api in vercel function in production

393 0
cancel
Showing results for 
Search instead for 
Did you mean: 
brughwani
4 - Data Explorer
4 - Data Explorer

I using airtable's api by connecting it to front end in flutter and a javascript backend using vercel function,these functions are working in dev environment but not in production

function call in front end in flutter

Future<void> createRecord(String firstName, String lastName, String address, String phoneNumber, String password, String personalMobileNumber, String salary, String role) async {
final String url = 'crmvercelfun.vercel.app/api/addemployee';
// Prepare the data to be sent
final response = await http.post(
Uri.parse(url),
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode(
{ "fields":
{
"First Name": firstName,
"Last Name": lastName,
"Address": address,
"Phone Number": phoneNumber,
"Password": password,
"Personal Mobile Number": personalMobileNumber,
"Salary": salary,
"Role": role,
}
}
),

);

print(jsonEncode({
"First Name": firstName,
"Last Name": lastName,
"Address": address,
"Phone Number": phoneNumber,
"Password": password,
"Personal Mobile Number": personalMobileNumber,
"Salary": salary,
}));
if (response.statusCode == 200) {
 
print('Record created successfully: ${response.body}');
} else {
print('Failed to create record: ${response.statusCode} ${response.body}');
}
}
 
implementation in vercel function 
 
// import the required modules
const Airtable = require('airtable');

// Configure the Airtable base with your API key and base ID
const base = new Airtable({apiKey: process.env.AIRTABLE_API_KEY}).base(process.env.AIRTABLE_BASE_ID);


module.exports = async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');

if (req.method === 'OPTIONS') {
res.status(200).end();
return;
}

if (req.method !== 'POST') {
res.status(405).send('Method Not Allowed');
return;
}

try {
console.log('Request body:', req.body);

if (!req.body.fields) {
throw new Error('Missing fields object in request body');
}

// Extract the fields from the request body
const {
'First Name': firstName,
'Last Name': lastName,
'Role': role,
'Phone Number': phone,
'Password': password,
'Address': address,
'Personal Mobile Number': personalPhoneNumber,
'Salary': salary
} = req.body.fields;

// Combine the extracted fields into the data object
const data = {
"First Name": firstName,
"Last name": lastName,
"Role": role,
"Phone": phone,
"password": password,
"address": address,
"personal phone number": personalPhoneNumber,
"Salary": salary,
};

console.log('Data to be inserted:', data);

// Insert the data into the 'Employee' table
const record = await base('Employee').create(data, {typecast: true});

// If successful, send the ID of the created record as the response
res.status(200).json({ id: record.getId() });
} catch (error) {
console.error('Detailed error:', {
message: error.message,
stack: error.stack,
name: error.name
});
res.status(500).json({ error: error.message || 'Internal Server Error' });
}
};

 

0 Replies 0