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

C#中使用RestSharp向Taxdoo API的POST请求添加Body参数

解决Taxdoo API POST请求BadRequest问题(RestSharp版本)

你遇到的核心问题是请求体结构不匹配:Taxdoo API期望的是一个包含products数组的对象,但你当前生成的JSON直接是数组,缺少外层的products键,导致API无法解析请求,返回BadRequest。

1. 创建请求体包装类

首先定义一个类来匹配API要求的结构,外层包含products字段:

public class ProductRequest
{
    // 若需明确指定JSON键名,可添加JsonProperty特性
    // [JsonProperty("products")]
    public List<MyProduct> Products { get; set; }
}

// 确保MyProduct类属性与API字段对应(RestSharp默认会处理驼峰命名映射)
public class MyProduct
{
    // [JsonProperty("description")]
    public string Description { get; set; }
    // [JsonProperty("productIdentifier")]
    public string ProductIdentifier { get; set; }
    // [JsonProperty("commodityCode")]
    public string CommodityCode { get; set; }
    // [JsonProperty("purchasePrice")]
    public decimal PurchasePrice { get; set; }
    // [JsonProperty("currency")]
    public string Currency { get; set; }
}

2. 修改请求代码

替换手动序列化数组的逻辑,改用RestSharp的AddJsonBody自动生成正确格式的JSON,同时必须指定请求方法为POST(RestRequest默认是GET,这也是常见错误):

namespace ConnectToApi
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var endpoint = new Uri("https://sandbox-api.taxdoo.com/products");
            using (RestClient? restClient = new(endpoint))
            {
                // 明确指定请求方法为POST
                var request = new RestRequest(Method.Post);
                var authToken = ConfigurationManager.AppSettings["authToken"];
                request.AddHeader("AuthToken", authToken);
                       
                MyProduct productOne = new MyProduct
                {
                    Description = "Product 1",
                    ProductIdentifier = "GBP-120",
                    CommodityCode = "123456",
                    PurchasePrice = 40,
                    Currency = "USD"
                };
                MyProduct productTwo = new MyProduct
                {
                    Description = "Product 2",
                    ProductIdentifier = "GBP-125",
                    CommodityCode = "789101112",
                    PurchasePrice = 20,
                    Currency = "USD"
                };
                
                List<MyProduct> productList = new();
                productList.Add(productOne);
                productList.Add(productTwo);
                
                // 用包装类构建正确的请求体结构
                var requestBody = new ProductRequest
                {
                    Products = productList
                };
                
                // 让RestSharp自动序列化JSON,无需手动处理
                request.AddJsonBody(requestBody);
                
                var response = restClient.Execute(request);
                // 可添加日志或输出查看响应内容,方便后续排查
                Console.WriteLine(response.Content);
            }
        }
    }
}

关键说明

  • 请求方法必须为POST:RestRequest默认是GET请求,GET携带请求体不符合HTTP规范,会被API拒绝。
  • 请求体结构匹配:修复后生成的JSON会和API要求的格式一致,外层包含products键,内部是产品数组。
  • 避免手动序列化:使用AddJsonBody让RestSharp自动处理序列化,能减少格式错误,同时自动完成驼峰命名映射(比如类的Products属性会序列化为JSON的products)。

新手学习建议

  • 先掌握JSON基础结构:区分对象({})和数组([])的差异,能快速识别API文档里的请求体格式要求。
  • 重点学习RestSharp核心用法:请求方法指定、请求体序列化、请求头设置这三个部分是API调用的基础。
  • 调试技巧:打印请求的实际JSON内容(可序列化requestBody查看),对比API要求的格式,快速定位结构问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 05:01:08