I’m still learning about working with APIs and JSON data in C#. I managed to get single record creation working with Airtable’s API, but now I need help sending multiple records at once.
Here’s my current working code for single records:
public static async Task SendSingleRecord()
{
using (var client = new HttpClient())
{
using (var requestMsg = new HttpRequestMessage(new HttpMethod("POST"), "https://api.airtable.com/v0/MYAPPID/Podcasts"))
{
requestMsg.Headers.TryAddWithoutValidation("Authorization", "Bearer MYTOKEN");
requestMsg.Content = new StringContent("{\"records\": [{ \"fields\": { \"FeedURL\": \"https://feeds.example.com/rss/feed.xml\" }}]}");
requestMsg.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var result = await client.SendAsync(requestMsg);
}
}
}
I created these models to handle bulk data:
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace PodcastManager
{
public class BulkDataModel
{
public class DataFields
{
[JsonPropertyName("FeedURL")]
[JsonInclude]
public string FeedURL { get; set; }
}
public class DataRecord
{
[JsonPropertyName("fields")]
[JsonInclude]
public DataFields fields { get; set; }
}
public class RootObject
{
[JsonPropertyName("records")]
[JsonInclude]
public List<DataRecord> records { get; set; }
}
}
}
The JSON structure I’m trying to create looks like this:
{
"records": [
{
"id": "rec123ABC",
"fields": {
"FeedURL": "https://feeds.example.com/podcast1/rss.xml"
}
},
{
"id": "rec456DEF",
"fields": {
"FeedURL": "https://feeds.example.com/podcast2/rss.xml"
}
}
]
}
I’m stuck on how to populate my model from a DataGridView or List and serialize it properly for the API request. Any guidance would be helpful.