Hi!
I created API, but now I want send object with token and get result from server. Maybe my code is wrong I can't get result from server side. Here my code:
public List<string> Get_Application()
{
List<string> applicationList = new List<string>();
applicationList.Add("30");
applicationList.Add("0");
applicationList.Add("Consumer");
applicationList.Add("Dushanbe");
applicationList.Add("South West Region");
applicationList.Add("7000");
applicationList.Add("10");
applicationList.Add("1");
applicationList.Add("22");
applicationList.Add("0.10");
var client = new RestClient($"https://192.168.0.21:4455/api/Test/InsertUser/?{applicationList}&token=CBNL6vHAT8Q2d2rxfpUFrjk012Kgm89j");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
var json = JsonConvert.SerializeObject(applicationList);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", json, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return applicationList;
}
// Model Data Fields
namespace TestAPI.Models
{
public class Data
{
[JsonPropertyName("age")]
public string age { get; set; }
[JsonPropertyName("gender")]
public string gender { get; set; }
[JsonPropertyName("sector")]
public string sector { get; set; }
[JsonPropertyName("district")]
public string district { get; set; }
[JsonPropertyName("region")]
public string region { get; set; }
[JsonPropertyName("amount")]
public string amount { get; set; }
[JsonPropertyName("duration")]
public string duration { get; set; }
[JsonPropertyName("credit_history_count")]
public string credit_history_count { get; set; }
[JsonPropertyName("int_rate")]
public string int_rate { get; set; }
[JsonPropertyName("threshold")]
public string threshold { get; set; }
}
// Objects send to API
public class SendToTest
{
[JsonPropertyName("token")]
public string token { get; set; }
[JsonPropertyName("data")]
public Data data { get; set; }
}
}
// Controller
namespace TestAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
[HttpPost("InsertUser")]// My problem between controller applicationList and token
public async Task<ActionResult> PostToTest([FromBody] SendToTest sendToTest)
{
var url = "https://test.azurewebsites.net/api/testapi"; // I haven't problem with this line api because I sent data and token manual got result
var client = new RestClient(url);
var request = new RestRequest(url, Method.Get);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", JsonConvert.SerializeObject(sendToTest), ParameterType.RequestBody); // Here I full data manual got result
RestResponse response = await client.ExecuteAsync(request);
if (response.Content == null)
{
// Can this happen?
}
else
{
var testDeserializedClass = JsonConvert.DeserializeObject<ResponseFromTest>(response.Content);
return Ok();
}
return Ok();
}
}
}