Mehrdad Mehrdad - 1 year ago 43
HTTP Question

HTTP status code for associating resources

Suppose I have 2 resources (models) in my service:

Degree
,
Course
.

When a user sends a post request to the rest API
https://example.com/degrees/degreeId/courses


with data like
[courseId => 2]

It associates
Degree
with id 1 to a
Course
with id 1.
What should status code be for representing this update?

Should I return
200
or
201
.

Also what do you suggest for response body? The updated
Degree
along with associated
Courses
?

Answer Source

I am not sure about the big picture of your API. It somehow seems that a specific course (the ID of which you provide in the request body) should be its own resource.

However given the description, I see two possibilities:

1) Create a new resources, such as

/degrees/1/course/2

This resource could for example be used to issue a later DELETE request. In this case you can return 201 and include a Link to the newly created resource.

2) If there is no such resource, 200 seems to be the only applicable positive response.

Option 1 seems preferable to me.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download