Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
635 views
in Technique[技术] by (71.8m points)

collections - Which HTTP code to use for an empty subresource in a REST API?

Let's say I've a resource articles at /articles.

These articles may have related articles, so I fetch them by GETting /articles/{id}/related.

What should I return is there is no related articles?

I can think of:

  • 404 Not Found, maybe with an empty collection
  • 204 No Content
  • 200 Found with an empty collection

Any advices? (please give arguments)

By the way, it may applies to pagination. If I request page 3 of 2, then the page 3 will return an empty set, should It be a 404?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

404 is not what you are looking for. It's an error condition. Your case is not an error. The client currently doesn't know if there are any related articles and wants to know. That is not an error.

204 is not appropriate either. RFC 2616 states:

  The server has fulfilled the request but does not need to return an
  entity-body, and might want to return updated metainformation. The
  response MAY include new or updated metainformation in the form of
  entity-headers, which if present SHOULD be associated with the
  requested variant.

204 doesn't specify that there are no related articles. It just says that the server doesn't need to send the data.

200 with empty collection on the other hand would satisfy your need.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...