HTTP status code in GET Method of REST api design

huangapple go评论89阅读模式
英文:

HTTP status code in GET Method of REST api design

问题

我正在学习有关REST API设计的最佳实践,并编写了一个处理GET /cities HTTP/1.1查询的函数。

这个函数包含一个城市的结构体数组,其中存储了多个城市的城市名称和城市代码。

以下是代码示例:

func FindCitiesHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json;charset=UTF-8")

    if len(cities) == 0 {
        w.WriteHeader(404)
        return
    }

    if err := json.NewEncoder(w).Encode(cities); err != nil {
        /* 在这里该怎么处理? */
    }

    w.WriteHeader(200)
}

现在,当我开始思考这个函数可能的结果时,我发现了以下情况:

  1. 它成功地将所有城市作为JSON响应返回。所以我返回200的HTTP状态码。

  2. 城市列表为空。所以没有内容可返回。所以我返回404(资源未找到)。

  3. 它即将返回所有城市的JSON响应,但在JSON编码过程中出现了问题。现在我在这里感到困惑,我该如何处理这种情况。

我的意思是,如果:

  1. 如果你的业务/应用逻辑出现了错误/异常。
  2. 如果数据访问逻辑发现了一些问题(比如无法连接到数据库)。

你们能否帮助我提供在这些情况下遵循的最佳实践?

英文:

I am learning about the best practices involved in REST API design and wrote a function which handles the GET /cities HTTP/1.1 query.

This function contains cities which is a struct array that holds the cityname, citycode of multiple cities.

Below is the code

func FindCitiesHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json;charset=UTF-8")

    if len(cities) == 0 {
            w.WriteHeader(404)
            return
    }

    if err := json.NewEncoder(w).Encode(cities); err != nil {
            /* what to do here? */
    }

    w.WriteHeader(200)    

}

Now when I started thinking about the possible outcomes of this function. I found these situations.

  1. It returns all the cities properly as JSON response successfully. So I return 200 http status.

  2. The list of cities is empty. So there is nothing to return. So I return 404 (resource not found)

  3. It is about to return JSON response of all cities, but something gone wrong during JSON encoding. Now I am confused here, how do I deal with this situation.

I mean how do you convey message properly to user, if

  1. If your business/application logic had some error/exception.
  2. If data access logic found some issues. (say connection to database is not reachable)

Could you guys please help me to suggest best practices you followed in these situations?

答案1

得分: 2

  1. 200 是正确的。
  2. 404 可能正确;城市列表可以为空,但仍然存在。(思考:长度为0的数组仍然是一个数组。)你应该返回 200。只有当城市列表在你的服务器上不存在时,才应该返回 404(换句话说,这在你的 API 中可能永远不会发生)。
  3. 如果遇到内部服务器错误,比如 JSON 编组错误,应该返回一个内部服务器错误,状态码为 500。
英文:
  1. 200 is correct
  2. 404 is probably not correct; A list of cities can be empty, and still exist. (Think: A 0-length array is still an array.) You should probably return 200. You would only return 404 if the list of cities doesn't exist on your server (in other words, that should probably never happen with your API).
  3. If you experience an internal server error, such as with marshaling JSON, you should return an Internal Server Error, status 500.

huangapple
  • 本文由 发表于 2017年2月28日 16:47:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/42503982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定