Issue
I am new to .NET and the MVC architecture. I am working on a mini project to create API for students information processing. However, I encountered the following error while attempting to create a collection of new students using curl:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":[REDACTED],"errors":{"$":["The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[StudentsManagementSystem.Entity.CreateStudentEntity]. Path: $ | LineNumber: 0 | BytePositionInLine: 1."]}}
Here’s the Controller class that should handle the request:
namespace StudentsManagementSystem.StudentController{
[ApiController]
[Route("api/student/Collections")]
public class StudentCollectionController: ControllerBase
{
private IStudentRepository _students;
private IMapper _mapper;
public StudentCollectionController(IMapper mapper, IStudentRepository student)
{
_mapper = mapper;
_students = student;
}
[HttpPost]
public ActionResult<IEnumerable<Student>> CreateStudentCollection(IEnumerable<CreateStudentEntity> newStudents)
{
Console.WriteLine("DEBUGG");
if (newStudents.Count<CreateStudentEntity>()< 1) return BadRequest();
var map = _mapper.Map<IEnumerable<Student>>(newStudents);
foreach(var student in map)
{
_students.AddStudent(student);
}
return Ok();
}
}
The CreateStudentEntity
Class:
namespace StudentsManagementSystem.Entity{
public class CreateStudentEntity
{
public string StudentName { get; set; }
public string MatricNo { get; set; }
public string Level { get; set; }
public string Dept { get; set; }
public DateTime DoB { get; set; }
public int YOA { get; set; }
public ICollection<Course> Results { get; set; } = new List<Course>();
}
Student
Entity class:
namespace StudentsManagementSystem.Entity{
public class Student
{
public string StudentName { get; set; }
public int StudentId { get; set; }
public string MatricNo { get; set; }
public string Level { get; set; }
public string Dept { get; set; }
public DateTime DoB { get; set; }
public int YOA { get; set; }
public ICollection<Course> Results { get; set; } = new List<Course>();
}
The curl
request:
curl -X POST http://localhost:5000/api/student/collections -H "Content-Type:application/json" -d "{{}}"
Note:DEBUGG
was not written on the console.
Kindly help a n00b 🙁
Solution
The value you’re sending in the cURL request – {{}}
– isn’t valid JSON and doesn’t represent an array. Arrays in JSON are represented by square brackets []
and objects by curly brackets {}
.
So, if you want to pass an empty array to the controller you’d use []
.
If you want to pass an array containing an empty object it would be [{}]
, eg:
curl -X POST http://localhost:5000/api/student/collections -H "Content-Type:application/json" -d "[{}]"
To pass CreateStudentEntity
objects in the array you’d need to include the properties, eg:
[{"MatricNo": "ABC123", "StudentId": 123, ...}, {object2...}, ...]
Answered By – haldo
Answer Checked By – David Goodson (BugsFixing Volunteer)