I am stuck at the end to find code for
(Send a Request with LIST & Parameters to a restAPI Post with Json in WinForm C#)
i have local Web Server with WEB Api POST Method
with sending Parameters, the address is : https://localhost:44328/api/hitung
with Postman application
i send Request Parameters like this POST > Body > RAW > JSON
and have some Return Response, from with this 4 Parameters,
{
"retailercode": "CJF00055",
"orderitems": [
{
"rownumber": 1,
"productcode": "100535",
"qty": 5
}
]
}
And in the Postman will return Response like this =
{
"retailercode": "CJF00055",
"orderitems": [
{
"rownumber": 1,
"productcode": "100535",
"qty": 5.0,
"orderprice": 8727.0,
"percentdisc": "D1.5",
"disreg": 1.5,
"valuediscreg": 654.525,
"totaldisc": 654.525
}
]
}
what i have tried to code :
but still get Error from my Massage Box (“Message”: “An error has occurred.”)
the problem is i dont know how to create (Parameters List Request) in my code C#
Please can someone try to help me to solved my problem?
my model List Class
public class Root0
{
public string retailercode { get; set; }
public List<orderitem0> orderitems { get; set; }
}
public class orderitem0
{
public int rownumber { get; set; }
public string productcode { get; set; }
public decimal qty { get; set; }
public decimal orderprice { get; set; }
public string percentdisc { get; set; }
public decimal disreg { get; set; }
public decimal valuediscreg { get; set; }
public decimal totaldisc { get; set; }
my code have some Error.
using System;
using System.Threading.Tasks;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Windows.Forms;
public async void coba5()
{
try
{
string url = "https://localhost:44328/api/hitung";
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
var requestData = new Root0 @"{""RetailerCode"" : ""CJF00055"",
""orderitems"": [
{
""rownumber"": 1,
""productcode"": ""100535"",
""qty"": 5
}]}";
string json = JsonSerializer.Serialize(requestData);
var content = new StringContent(json, Encoding.UTF32, "application/json");
var response = await client.PostAsync(url, content);
string responseContent = await response.Content.ReadAsStringAsync();
MessageBox.Show(responseContent);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
coba5();
}