是否可能有非空响应的 204 状态代码

Is that possible to have 204 status code with non-empty response

我正在使用 http 模块。前端开发团队在没有结果时询问我,发送一个空列表和状态代码 204 的响应。我试过这个:

AllPosts  := logic.MergedSearchSearchPost(params)

if len(AllPosts.Posts) == 0 {
    w.WriteHeader(http.StatusNoContent)
    json.NewEncoder(w).Encode(AllPosts)
}

在这种情况下,AllPosts 是这样的:

{
    "total": 0,
    "is_finished": true,
    "query_id": "c2x86XSZaU",
    "posts": null
}

问题是我设置状态码为204后无法发送任何东西,所以响应为空。我想用 204 状态码发送上面的 AllPosts。有什么办法吗?

The Front-End developers team asking me when there are no results, send a response with an empty list and status code 204

这两个相互矛盾。空列表是一些内容。 这直接违反了 HTTP standard:

A 204 response is terminated by the first empty line after the header fields because it cannot contain a message body.

Go HTTP 库不允许您这样做。