You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用C#及REST API批量向Airtable提交记录?

批量提交Airtable记录实现方案

1. 从字符串列表初始化模型

如果待提交的URL已存储在List<string>中,可直接遍历列表构建批量提交模型:

// 示例URL列表
List<string> urlList = new List<string>
{
    "https://example.com/rss-feed-1",
    "https://example.com/rss-feed-2",
    "https://example.com/rss-feed-3"
};

// 构建批量提交模型
var batchModel = new StreamsPOSTModel.Root
{
    records = urlList.Select(url => new StreamsPOSTModel.Record
    {
        fields = new StreamsPOSTModel.Fields
        {
            URL = url
        }
    }).ToList()
};

2. 从DataGridView初始化模型

假设DataGridView包含名为"URL"的列,遍历行数据提取URL并构建模型:

var batchModel = new StreamsPOSTModel.Root
{
    records = new List<StreamsPOSTModel.Record>()
};

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    // 跳过新增行和空行
    if (row.IsNewRow) continue;
    
    string url = row.Cells["URL"].Value?.ToString();
    if (!string.IsNullOrWhiteSpace(url))
    {
        batchModel.records.Add(new StreamsPOSTModel.Record
        {
            fields = new StreamsPOSTModel.Fields
            {
                URL = url
            }
        });
    }
}

3. 序列化模型并发送批量请求

使用System.Text.Json将模型序列化为JSON,替换手动拼接的请求体,发送批量POST请求:

public static async Task SubmitBatchRecords(StreamsPOSTModel.Root batchModel)
{
    using (var httpClient = new HttpClient())
    {
        using (var request = new HttpRequestMessage(HttpMethod.Post, "https://api.airtable.com/v0/APPID/Streams"))
        {
            // 添加Airtable授权头
            request.Headers.TryAddWithoutValidation("Authorization", "Bearer APIKEY");
            
            // 序列化模型为JSON字符串
            string jsonPayload = JsonSerializer.Serialize(batchModel);
            request.Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
            
            var response = await httpClient.SendAsync(request);
            
            // 可选:验证请求结果
            response.EnsureSuccessStatusCode(); // 请求失败时抛出异常
            string responseBody = await response.Content.ReadAsStringAsync();
            Console.WriteLine("提交结果:" + responseBody);
        }
    }
}

4. 完整调用示例

结合上述步骤,完整的批量提交流程示例:

// 从列表提交
var urlList = new List<string> { "url1", "url2", "url3" };
var modelFromList = new StreamsPOSTModel.Root
{
    records = urlList.Select(u => new StreamsPOSTModel.Record { fields = new StreamsPOSTModel.Fields { URL = u } }).ToList()
};
await SubmitBatchRecords(modelFromList);

// 从DataGridView提交
var modelFromDgv = new StreamsPOSTModel.Root { records = new List<StreamsPOSTModel.Record>() };
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (!row.IsNewRow)
    {
        string url = row.Cells["URL"].Value?.ToString();
        if (!string.IsNullOrWhiteSpace(url))
        {
            modelFromDgv.records.Add(new StreamsPOSTModel.Record { fields = new StreamsPOSTModel.Fields { URL = url } });
        }
    }
}
await SubmitBatchRecords(modelFromDgv);

注意事项

  • 替换代码中的APPID和APIKEY为你的Airtable实际凭证
  • Airtable API默认限制单次批量提交最多10条记录,超过需拆分请求
  • 需过滤空URL或无效数据,避免提交错误请求

内容的提问来源于stack exchange,提问作者Abanoub Zak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 15:15:43