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);
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 -Withdrawaland useList<Withdrawal>. Unfortunately that still doesn't match the structure of the curl request and you're going to need another DTO.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
Of course, you'll have to add any additional properties you want to include in the request.