Let's say I have two objects: Student
and Homework
. Homework
has Student
as fk:
CREATE TABLE student (
id serial PRIMARY KEY,
name varchar(100)
)
CREATE TABLE homework (
id serial PRIMARY KEY,
topic varchar(255),
student_id int REFERENCES student(id)
)
Is there any convention for what is the right way to structure the API endpoints for CRUD?
If I want to create a new homework for a student, I could send a json body with student id
{
"student_id": 1,
"topic": "topic
}
to POST https://website.com/api/v1/homework
.
Or I could send
{
"topic": "topic
}
to POST https://website.com/api/v1/students/{student_id}/homework
and take student id from URL.
In second case I would be sending a POST request with incomplete fields and in first case I would have one extra endpoint (since I would need /students/{id}/homework
anyway to fetch particular student's homework.)