Help

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

907 3
cancel
Showing results for 
Search instead for 
Did you mean: 
perry
4 - Data Explorer
4 - Data Explorer

 

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 : 

perry_2-1692123870797.png

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

perry_1-1692123159802.png

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"
}
}'
perry_3-1692124062631.png

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 3
Ron_Williams
6 - Interface Innovator
6 - Interface Innovator

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

perry
4 - Data Explorer
4 - Data Explorer

Thanks. How do I do that exactly ? 

Would it be : 

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

]


JSON.stringify(raw)

Ron_Williams
6 - Interface Innovator
6 - Interface Innovator

@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();