Golang发送带有状态的JSON响应的正确方法

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

Golang proper way to send json response with status

问题

如何在响应体中发送带有状态码的json响应。

我的代码

func getUser(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	var user []User
	result := db.Find(&user)
	response := map[string]interface{}{
		"status":     "success",
		"statusCode": 200,
		"data":       result,
	}
	json.NewEncoder(w).Encode(response)
}

我的当前结果:

[
	{
		"name" : "test",
		"age" : "28",
		"email":"test@gmail.com"
	},
	{
		"name" : "sss",
		"age" : "60",
		"email":"ss@gmail.com"
	},
	{
		"name" : "ddd",
		"age" : "30",
		"email":"ddd@gmail.com"
	},
]

但是我需要发送带有status码的响应,像这样:

{
	"status" : "success",
	"statusCode" : 200,
	"data" : [
		{
			"name" : "test",
			"age" : "28",
			"email":"test@gmail.com"
		},
		{
			"name" : "sss",
			"age" : "60",
			"email":"ss@gmail.com"
		},
		{
			"name" : "ddd",
			"age" : "30",
			"email":"ddd@gmail.com"
		},
	]
}
英文:

How to send json response with status code in response body.

My code

func getUser(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	var user []User
	result := db.Find(&user)
	json.NewEncoder(w).Encode(result)
}

My result now:

[
	{
		"name" : "test",
		"age" : "28",
		"email":"test@gmail.com"
	},
	{
		"name" : "sss",
		"age" : "60",
		"email":"ss@gmail.com"
	},
	{
		"name" : "ddd",
		"age" : "30",
		"email":"ddd@gmail.com"
	},
]

But I need to send the response with status code like this

{
	status : "success",
	statusCode : 200,
	data : [
		{
			"name" : "test",
			"age" : "28",
			"email":"test@gmail.com"
		},
		{
			"name" : "sss",
			"age" : "60",
			"email":"ss@gmail.com"
		},
		{
			"name" : "ddd",
			"age" : "30",
			"email":"ddd@gmail.com"
		},
	]
}

答案1

得分: 5

如果你想要不同的 JSON,可以将不同的对象传递给 Encode 方法。

type Response struct {
    Status     string `json:"status"`
    StatusCode int    `json:"statusCode"`
    Data       []User `json:"data"`
}

func getUser(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    var user []User
    result := db.Find(&user)
    json.NewEncoder(w).Encode(&Response{"success", 200, result})
}

或者使用 map

json.NewEncoder(w).Encode(map[string]interface{}{
    "status":     "success",
    "statusCode": 200,
    "data":       result,
})
英文:

If you want different json, pass a different object to Encode.

type Response struct {
    Status       string `json:"status"`
    StatucCode   int    `json:"statusCode"`
    Data         []User `json:"data"`
}

func getUser(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    var user []User
    result := db.Find(&user)
    json.NewEncoder(w).Encode(&Response{"success", 200, result})
}

Or use a map:

json.NewEncoder(w).Encode(map[string]interface{}{
    "status": "success", 
    "statusCode": 200, 
    "data": result,
})

huangapple
  • 本文由 发表于 2021年10月1日 15:21:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/69401999.html
匿名

发表评论

匿名网友

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

确定