Struggling to add Airtable record using .NET: How to fix POST request?

I’m having trouble adding a new record to Airtable using my .NET test app. Reading data works fine but I can’t seem to insert new records and keep getting a ‘422 Unprocessable Entity’ error.

Here’s a simplified version of my approach:

Dim client As New HttpClient()
client.DefaultRequestHeaders.Add("Authorization", "Bearer mySecretKeyHere")

Dim content As New StringContent("{""fields"": {""Column1"": ""Value1"", ""Column2"": ""Value2""}}", Encoding.UTF8, "application/json")

Dim response = Await client.PostAsync("https://api.airtable.com/v0/myBaseId/myTableName", content)

If Not response.IsSuccessStatusCode Then
    Console.WriteLine($"Error: {response.StatusCode}")
End If

Am I formatting the JSON data incorrectly or missing something in the headers? I’d appreciate any guidance on resolving this issue.

I encountered a similar issue when working with Airtable’s API in .NET. The 422 error often indicates a problem with the request payload. Here are a few things to check:

Ensure your column names in the JSON exactly match those in Airtable, including case sensitivity.

Verify that the data types of the values you’re sending match the field types in Airtable.

Double-check that your base ID and table name are correct.

Try adding a “Content-Type” header explicitly, for example, client.DefaultRequestHeaders.Add(“Content-Type”, “application/json”).

Consult Airtable’s API documentation to compare your request structure with their examples.

If these suggestions don’t resolve the issue, consider using tools like Fiddler or Postman to inspect the full request and response for further insights.

hey mate, had similar probs. try adding ‘Content-Type’ header explicitly:

client.DefaultRequestHeaders.Add(“Content-Type”, “application/json”)

also double check ur column names match exactly in airtable (case sensitive). if still no luck, use Postman to test the API directly. it helps pinpoint where things go wrong. good luck!

I’ve dealt with Airtable’s API before, and I can tell you it can be a bit finicky. One thing that helped me was using the Airtable.net NuGet package instead of raw HTTP requests. It simplified the process a lot.

If you prefer sticking with HttpClient, make sure you’re using the correct endpoint. It should be something like ‘https://api.airtable.com/v0/[BASE_ID]/[TABLE_NAME]’. Also, double-check your API key - sometimes it’s easy to miss a character.

Another tip: log the full response body when you get an error. Airtable usually provides more detailed error messages that can point you in the right direction. Something like:

If Not response.IsSuccessStatusCode Then
    Dim responseBody = Await response.Content.ReadAsStringAsync()
    Console.WriteLine($"Error: {response.StatusCode}, Body: {responseBody}")
End If

This has saved me hours of debugging in the past. Good luck with your integration!