I’m having trouble with my Airtable API integration. When I try to POST a new record, I keep getting a 422 error. Here’s what I’ve done so far:
I set up a method to insert a record:
async Task AddNewEntry(Item data)
{
string endpoint = "https://api.airtable.com/v0/myappid/Items";
string apiKey = "myapikey";
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
try
{
var jsonData = JsonConvert.SerializeObject(data);
var content = new StringContent(jsonData);
var response = await httpClient.PostAsync(endpoint, content);
if (response.IsSuccessStatusCode)
Console.WriteLine("Entry added successfully");
else
Console.WriteLine($"Error: {response.StatusCode}");
}
catch (Exception e)
{
Console.WriteLine($"Exception: {e.Message}");
}
}
}
My Item class looks like this:
class Item
{
public string Name { get; set; }
public string ImageUrl { get; set; }
}
When I test with Postman, it works fine. But my C# code always returns a 422 error. What am I missing? Any help would be appreciated!