I want to pass nested parameters with HTTP client POST. How can I achieve this I have the following code:
string credentials = JsonConvert.SerializeObject(new
{
ApiKey= "123456",
});
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://test/tokens");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
HttpRequestMessage request = new HttpRequestMessage();
request.Method = HttpMethod.Post;
request.Content = new StringContent(credentials, Encoding.UTF8, "application/json");
My JSON file looks like this. How can I pass all these nested parameters with HTTP client post.
{
"OrderTotal":null,
"MerchantCode":"Test1",
"MerchantKey":"Test2",
"ServiceCode":"Testservice1",
"Phone":"123-456-7899",
"Email":"test@test.com",
"CustomerId" : null,
"CompanyName" : "",
"CustomerAddress":{
"Name":"Thomas jefferson",
"Address1":"123 test drive",
"Address2":"",
"City":"LCK",
"State":"MI",
"Zip":"12345",
"Country":"US"
},
"LineItems":
[
{
"Sku":"TEST",
"Description":"Test",
"UnitPrice":100.00,
"Quantity":1
}
]
}
I defined these two classes too:
public class MainParametrs
{
public string MerchantCode { get; set; }
public string MerchantKey { get; set; }
public string ServiceCode { get; set; }
public string UniqueTransId { get; set; }
public string LocalRef { get; set; }
public string SuccessUrl { get; set; }
public string FailureUrl { get; set; }
public string DuplicateUrl { get; set; }
public string CancelUrl { get; set; }
public string Phone { get; set; }
public string CustomerAddress { get; set; }
public string LineItems { get; set; }
}
public class CustomerAddressParam
{
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
}
Any help will be highly appreciated.