在Chi路由器中,使用`w.Write`和`Render.JSON`发送响应有什么区别?

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

Golang sending a response as w.Write vs Render.JSON in Chi Router?

问题

文档似乎没有帮助,我找不到很多描述render.JSONW.Write([]byte)在Go语言中的区别的帖子。我们有一个简单的chi路由器,它发送一个POST请求并返回数据。我以为唯一的区别是自动设置内容类型,但是当我使用render.JSON时,它会删除键。

请问有人能用简单的语言告诉我是否有区别以及使用renderwrite的好用例吗?有人有个人经验或建议吗?我已经阅读了文档,并看到了很多不同的响应发送方式。

//当前首选项,因为我可以定义头部等
w.WriteHeader(resp.StatusCode)
w.Header().Set("Content-Type", "application/json")
json := []byte(body)
w.Write(json)

//与之相比(设置内容类型,但删除了键*,我不确定为什么)
render.JSON(w, r, interface)
英文:

The documentation did not seem to help, and I could not find a lot of post that described the differences in go lang with render.JSON vs a W.Write([]byte). We have a simple chi router that makes a post request and returns the data. I thought the only difference was auto setting content type, but when I use render.JSON it removes the keys.

Can anyone please tell me in a few human words if there is a difference and a good use case to use render vs write? Does anyone have any personal exp, or advice regarding this? I have read the documents, and seen response sent many different ways.

//Current Pref as I can define the headers, etc. 
	w.WriteHeader(resp.StatusCode)
	w.Header().Set("Content-Type", "application/json")
	json := []byte(body)
	w.Write(json)



//vs (sets content type, but removes the keys* and I am not sure why) 
render.JSON(w,r,interface)

</details>


# 答案1
**得分**: 1

请看 [render.JSON 的代码](https://github.com/go-chi/render/blob/3215478343fbc559bd3fc08f7031bb134d6bdad5/responder.go#L93-L107)。该函数将参数编码为 JSON,设置 JSON 内容类型头,并将 JSON 写入响应。应用程序可以在调用 render.JSON 之前设置响应头。应用程序可以通过调用 [render.Status](https://pkg.go.dev/github.com/go-chi/render#Status) 来设置响应状态,然后再调用 render.JSON。

[ResponseWriter.Write](https://pkg.go.dev/net/http#ResponseWriter.Write) 方法按原样将字节写入响应。

请注意 render.JSON 和 ResponseWriter.Write 之间的一个关键区别:前者将参数编码为 JSON,而后者按原样写入字节。

render.JSON 函数封装了应用程序常用的功能。如果方便的话,可以使用该函数。

<details>
<summary>英文:</summary>

See the [code for render.JSON](https://github.com/go-chi/render/blob/3215478343fbc559bd3fc08f7031bb134d6bdad5/responder.go#L93-L107).  The function encodes the argument to JSON, sets a JSON content type header and writes the JSON to the response. The application can set response headers before calling render.JSON. The application can set the response status by calling [render.Status](https://pkg.go.dev/github.com/go-chi/render#Status) before calling render.JSON.


The [ResponseWriter.Write](https://pkg.go.dev/net/http#ResponseWriter.Write) method writes bytes to the response as is.

Note a key difference between render.JSON and ResponseWriter.Write: The former function encodes the argument as JSON and the latter writes the bytes as is.

The render.JSON function encapsulates functionality commonly used by applications.  Use the function if you find it convenient.

</details>



huangapple
  • 本文由 发表于 2022年6月6日 23:39:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/72520107.html
匿名

发表评论

匿名网友

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

确定