Transform Airtable REST API POST request from cURL to C# or VB.NET implementation

I’m struggling to insert new records into Airtable using my VB.NET application. While I can retrieve data without issues, creating new entries keeps failing. I suspect there might be an issue with my JSON formatting or missing headers.

Dim headers As WebHeaderCollection = New WebHeaderCollection()
headers.Clear()
headers.Add("Authorization: Bearer keyABC123DEFXXXX")

Dim apiUrl As String = "https://api.airtable.com/v0/appXYZ789ABC123/Records"

Dim request As HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(apiUrl), HttpWebRequest)

request.ContentType = "application/json"
request.Headers = headers
request.Method = "POST"
request.Accept = "*/*"

Dim jsonData = "{""fields"": {""ProductName"": ""sample"", ""Category"": ""testing"", ""Status"": ""active""}}"

request.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(jsonData), 0, System.Text.Encoding.UTF8.GetBytes(jsonData).Count)

Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

Dim result As String = ""

Using reader As New StreamReader(response.GetResponseStream())
    result = reader.ReadToEnd()
End Using

MessageBox.Show(result)

The API returns “422 Unprocessable Entity” error consistently. What could be wrong with my request structure?

Quick debugging tip that’s saved me tons of time - add error handling to see what Airtable’s actually telling you. That 422 response usually has a detailed error message in the body.

Wrap your GetResponse() call in try-catch and read the error stream:

Try
    Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
    ' your success code here
Catch ex As WebException
    Using errorResponse = DirectCast(ex.Response, HttpWebResponse)
        Using reader As New StreamReader(errorResponse.GetResponseStream())
            MessageBox.Show(reader.ReadToEnd())
        End Using
    End Using
End Try

This shows you exactly what Airtable thinks is wrong - field validation problems, data type mismatches, base permissions, whatever. Beats guessing every time.

Had this exact problem six months ago with a VB.NET integration. You’re not closing the request stream after writing to it. After you write data to GetRequestStream, call Close() or Dispose() on that stream before getting the response. Without closing it, the request hangs and Airtable throws a 422. Add request.GetRequestStream().Close() right after your Write operation, or wrap it in a Using statement. Drove me crazy for weeks until I realized the stream wasn’t being terminated.

your JSON structure’s probably the issue - airtable wants the data wrapped in a “records” array. try {"records": [{"fields": {"ProductName": "sample", "Category": "testing", "Status": "active"}}]} instead. that 422 error usually means your payload format doesn’t match what they’re expecting.

check your base permissions too - i hit this exact issue when my api key didn’t have write access to that table. also, switch to httpclient instead of httpwebrequest. it handles stream management way better and you won’t run into these problems as much.

Had the same issue when I started with Airtable’s API. That 422 error usually means your request structure is messed up. Besides the JSON wrapping problem mentioned above, try explicitly setting the Content-Length header - that fixed it for me multiple times. Also check your table name in the URL - use the actual table name, not “Records”. I always add request.ContentLength = System.Text.Encoding.UTF8.GetByteCount(jsonData) before writing to the request stream. Fixes those weird 422 errors most of the time. And make sure your base ID and table name are URL-encoded properly, especially with spaces or special characters.