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

如何将支付网关CURL API转换为C# RestSharp并处理提款数组

问题描述

我正尝试将支付网关的CURL API转换为C# RestSharp代码。原CURL请求如下:

curl --location 'https://api.thePaymentGateway.io/v1/payout' \
--header 'Authorization: Bearer {{token}}' \
--header 'x-api-key: {{api-key}}' \
--header 'Content-Type: application/json' \
--data '{
    "ipn_callback_url": "https://thePaymentGateway.io",
    "withdrawals": [
        {
            "address": "TEmGwPeRTPiLFLVfBxXkSP91yc5GMNQhfS",
            "currency": "trx",
            "amount": 200,
            "ipn_callback_url": "https://thePaymentGateway.io"
        },
        {
            "address": "0x1EBAeF7Bee7B3a7B2EEfC72e86593Bf15ED37522",
            "currency": "eth",
            "amount": 0.1,
            "ipn_callback_url": "https://thePaymentGateway.io"
        },
        {
            "address": "0x1EBAeF7Bee7B3a7B2EEfC72e86593Bf15ED37522",
            "currency": "usdc",
            "amount": 1,
            "fiat_amount": 100,
            "fiat_currency": "usd",
            "ipn_callback_url": "https://thePaymentGateway.io"
        }
    ]
}'

我编写了以下C#代码,但仅能处理单个提款条目,无法通过Dictionary实现withdrawals数组结构,请问如何实现向接口发送withdrawals数组?

var client = new RestClient("https://api.thePaymentGateway.io/v1/payout");

var keyValues = new Dictionary<string, string>
    {
        { "address", "0x1EBAeF7Bee7B3a7B2EEfC72e86593Bf15ED37522"},
        { "currency", "usdttrc20"},
        { "amount", 200},
    };
//serialization using Newtonsoft JSON 
string JsonBody = JsonConvert.SerializeObject(keyValues);

var request = new RestRequest();
request.Method = Method.Post;
request.AddHeader("Authorization", "Bearer  {{token}}");
request.AddHeader("x-api-key", {{api-key}});
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", JsonBody, ParameterType.RequestBody);
RestResponse response = client.Execute(request);
解决方案

用Dictionary处理嵌套数组结构会非常繁琐,推荐通过强类型实体类或匿名类型来构建请求体,轻松实现withdrawals数组的序列化。

方案一:使用强类型实体类(推荐)

通过定义与JSON结构匹配的实体类,既能保证类型安全,也方便后续维护。

1. 定义实体类

// 单个提款条目类
public class WithdrawalItem
{
    public string address { get; set; }
    public string currency { get; set; }
    public decimal amount { get; set; }
    public string ipn_callback_url { get; set; }
    // 可选字段,设置为可空类型适配不同提款场景
    public decimal? fiat_amount { get; set; }
    public string fiat_currency { get; set; }
}

// 根请求体类
public class PayoutRequest
{
    public string ipn_callback_url { get; set; }
    public List<WithdrawalItem> withdrawals { get; set; }
}

2. 构建请求体并序列化

var client = new RestClient("https://api.thePaymentGateway.io/v1/payout");

// 构建多个提款条目
var withdrawalList = new List<WithdrawalItem>
{
    new WithdrawalItem
    {
        address = "TEmGwPeRTPiLFLVfBxXkSP91yc5GMNQhfS",
        currency = "trx",
        amount = 200,
        ipn_callback_url = "https://thePaymentGateway.io"
    },
    new WithdrawalItem
    {
        address = "0x1EBAeF7Bee7B3a7B2EEfC72e86593Bf15ED37522",
        currency = "eth",
        amount = 0.1m,
        ipn_callback_url = "https://thePaymentGateway.io"
    },
    new WithdrawalItem
    {
        address = "0x1EBAeF7Bee7B3a7B2EEfC72e86593Bf15ED37522",
        currency = "usdc",
        amount = 1m,
        fiat_amount = 100,
        fiat_currency = "usd",
        ipn_callback_url = "https://thePaymentGateway.io"
    }
};

// 组装根请求对象
var payoutRequest = new PayoutRequest
{
    ipn_callback_url = "https://thePaymentGateway.io",
    withdrawals = withdrawalList
};

// 序列化为JSON
string jsonBody = JsonConvert.SerializeObject(payoutRequest);

3. 发送请求

var request = new RestRequest();
request.Method = Method.Post;
request.AddHeader("Authorization", "Bearer {{token}}"); // 替换为实际token
request.AddHeader("x-api-key", "{{api-key}}"); // 替换为实际API密钥
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", jsonBody, ParameterType.RequestBody);

RestResponse response = client.Execute(request);

// 处理响应逻辑
if (response.IsSuccessful)
{
    string responseContent = response.Content;
    // 后续业务处理
}
else
{
    string errorMsg = response.ErrorMessage;
    // 错误处理
}

方案二:使用匿名类型(快速实现)

如果不想创建实体类,可以直接用匿名类型构建请求结构,同样能生成正确的JSON数组:

var client = new RestClient("https://api.thePaymentGateway.io/v1/payout");

// 用匿名类型构建完整请求体
var payoutRequest = new
{
    ipn_callback_url = "https://thePaymentGateway.io",
    withdrawals = new[]
    {
        new
        {
            address = "TEmGwPeRTPiLFLVfBxXkSP91yc5GMNQhfS",
            currency = "trx",
            amount = 200,
            ipn_callback_url = "https://thePaymentGateway.io"
        },
        new
        {
            address = "0x1EBAeF7Bee7B3a7B2EEfC72e86593Bf15ED37522",
            currency = "eth",
            amount = 0.1m,
            ipn_callback_url = "https://thePaymentGateway.io"
        },
        new
        {
            address = "0x1EBAeF7Bee7B3a7B2EEfC72e86593Bf15ED37522",
            currency = "usdc",
            amount = 1m,
            fiat_amount = 100,
            fiat_currency = "usd",
            ipn_callback_url = "https://thePaymentGateway.io"
        }
    }
};

string jsonBody = JsonConvert.SerializeObject(payoutRequest);

// 后续请求发送逻辑同方案一

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 10:15:05