Search code examples
c#restsharp

How to convert CURL API to C# RestSharp


I am trying to convert a CURL API to C# RestSharp. Below is the actual code

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"
        }
    ]
}'

I tried doing the below code, but what I could do is for single entry, the actual code is to take in array of "withdrawals" input to send to the endpoint: In my code I could not do the array with the Dictionary string.

Does anyone have an idea on how I can achieve sending the array of "withdrawals" to the endpoint?.

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);

Solution

  • The important bit is the structure you use to serialise the data and Dictionary<string, string> just won't work for you here.

    You could use List<Dictionary<string, string>> but all those magic strings is going to bite you at some point. It would be better to use a purpose built DTO - Withdrawal and use List<Withdrawal>. Unfortunately that still doesn't match the structure of the curl request and you're going to need another DTO.

    public class PayoutRequest
    {
        [JsonPropertyName("ipn_callback_url")]
        public string IpnCallbackUrl { get; set; }
    
        [JsonPropertyName("withdrawals")]
        public List<Withdrawal> Withdrawals { get; set; }
    }
    
    public class Withdrawal
    {
        [JsonPropertyName("address")]
        public string Address { get; set;}
    
        [JsonPropertyName("currency")]
        public string Currency { get; set;}
    
        [JsonPropertyName("amount")]
        public decimal Amount { get; set;}
    }
    

    Also, there's no need to manually serialise the data either, RestSharp will do that for you too.

    This code is untested and therefore may not be without bugs but you get the idea

        var dto = new PayoutRequest
        {
            IpnCallbackUrl = "https://thePaymentGateway.io",
            Withdrawals = new List<Withdrawal>
            {
                new Withdrawal
                {
                    Address = "0x1EBAeF7Bee7B3a7B2EEfC72e86593Bf15ED37522",
                    Currency = "usdttrc20",
                    Amount = 200m,
                }
            }
        };
    
        var request = new RestRequest();
        request.AddHeader("Authorization", $"Bearer  {token}");
        request.AddHeader("x-api-key", apiKey);
        request.AddJsonBody<PayoutRequest>(dto);
    
        var client = new RestClient("https://api.thePaymentGateway.io/v1/payout");
        RestResponse response = client.Post(request);
    

    Of course, you'll have to add any additional properties you want to include in the request.