Skip to main content

Post via API Gives Error 422 : "type": "INVALID_REQUEST_MISSING_FIELDS",


Forum|alt.badge.img+1
  • New Participant
  • 1 reply

 

ERROR : {
"error": {
"type": "INVALID_REQUEST_MISSING_FIELDS",
"message": "Could not find field \\"fields\\" in the request body"
},

I am attempting to insert data into a simple table : 4 cols called A,B,C,D datatype of cols is the same single string.

I compose my JSON body raw as follows : 

When I execute this via Postman I get the following error : 

If I execute the CURL command the insert works fine. This is the CURL statement : 


curl -X POST https://api.airtable.com/v0/apppXXXXXXXXl5tP/nXXXable -H "Authorization: Bearer patBTXXXXXXe71" -H "Content-Type: application/json" --data '{
"fields": {
"A": "AA",
"B": "AA",
"C": "AA",
"D": "AA"
}
}'

I have been working on this thing for 5hrs solid and cannot figure out where the issue is in regard to the JSON request body format. 

I am new to Airtable. 

Any help would be greatly appreciated as I am at a dead end.

Best

Perry

 




 

 

3 replies

Forum|alt.badge.img+9

Try changing it to body: JSON.stringify(raw) if you don't have it that way. 


Forum|alt.badge.img+1
  • Author
  • New Participant
  • 1 reply
  • August 16, 2023

Thanks. How do I do that exactly ? 

Would it be : 

raw = [
{
"A": "AA",
"B": "AA",
"C": "AA",
"D": "AA"
}

]


JSON.stringify(raw)


Forum|alt.badge.img+9

@perry 

It should be something like this:

// This function should be called to initiate the request.
async function postToAirtable() {
const data = {
records: [{
fields: {
"A": "AA",
"B": "BB",
"C": "CC",
"D": "DD"
}
}]
};

//replace your api key
const headers = new Headers({
"Authorization": "Bearer YOUR API KEY",
"Content-Type": "application/json"
});

const requestOptions = {
method: 'POST',
headers: headers,
body:JSON.stringify(data),
};

try {
// Your fetch code...replace the app and tbl with your info
const response = await fetch("https://api.airtable.com/v0/appXXXXXXX/tblXXXXXXXX", requestOptions);
 
// First, check if the response is OK. If not, throw an error with the response text.
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP error! Status: ${response.status}, Text: ${errorText}`);
}

const jsonResponse = await response.json();
console.log(jsonResponse);
} catch (error) {
console.error('There was a problem with the fetch operation:', error.message);
}
}
postToAirtable();
 

Reply