Reputation: 9073
POST method
SaveData([FromBody]MyDetails myDetails)
MyDetails is a class with
public int Id;
public int LocationId;
public List<Employee> Employee;
public bool Status;
Employee is a class with
public int EmployeeId;
public Name EmployeeName;
var values = new Dictionary<string, string>
{
{"Id",myDetails.Id.ToString()},
{"LocationId", myDetails.LocationId.ToString()},
{"Status", myDetails.Status.ToString()},
{"Employee", myDetails.Employee.ToString()} -- How do i send List Employee part of FormURLEncodedContent, i know this is wrong, i am having hard time getting this to work?
};
var encodedContent = new FormUrlEncodedContent(values);
var response = await client.PostAsync(url, encodedContent); //url points to POST method SaveData, which accepts MyDetails class object as parameter.
Upvotes: 1
Views: 3306
Reputation: 993
I have done like that. Here is my class details
public class AddTenantRequestdto
{
public IFormFile TenantLogo { get; set; }
public string TenantTitle { get; set; }
public List<string> ApplicationName { get; set; }
public bool EnableOTP { get; set; }
}
Here is my API
public async Task Tenant_Create_Success(AddTenantRequestdto addTenantRequest)
{
HttpClient Client = new HttpClient();
var tenantData = addTenantRequestdto.ToFormData();
var response = await _TestFixture.Client.PostAsync("http://localhost:61234/Tenants/CreateTenant", tenantData);
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
Extension Method is here, Add Newtonsoft.Json Package:-
public static class ExtensionMethods
{
public static IDictionary<string, string> ToKeyValue(this object metaToken)
{
if (metaToken == null)
{
return null;
}
// Added by me: avoid cyclic references
var serializer = new JsonSerializer { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
var token = metaToken as JToken;
if (token == null)
{
// Modified by me: use serializer defined above
return ToKeyValue(JObject.FromObject(metaToken, serializer));
}
if (token.HasValues)
{
var contentData = new Dictionary<string, string>();
foreach (var child in token.Children().ToList())
{
var childContent = child.ToKeyValue();
if (childContent != null)
{
contentData = contentData.Concat(childContent)
.ToDictionary(k => k.Key, v => v.Value);
}
}
return contentData;
}
var jValue = token as JValue;
if (jValue?.Value == null)
{
return null;
}
var value = jValue?.Type == JTokenType.Date ?
jValue?.ToString("o", CultureInfo.InvariantCulture) :
jValue?.ToString(CultureInfo.InvariantCulture);
return new Dictionary<string, string> { { token.Path, value } };
}
public static FormUrlEncodedContent ToFormData(this object obj)
{
var formData = obj.ToKeyValue();
return new FormUrlEncodedContent(formData);
}
}
Hope I have explained well, and it's working fine for me
Upvotes: 0
Reputation: 9073
Serializing the class objecto JSON
var content = JsonConvert.SerializeObject(myDetails); //myDetails is my class object.
var buffer = System.Text.Encoding.UTF8.GetBytes(content);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.PostAsync(url, byteContent);
Yielded the right result.
Upvotes: 1