将会话和JSON数据写入http.ResponseWriter

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

Writing Both session and JSON data to http.ResponseWriter

问题

在Go的net/http包中,是否可以同时发送会话和一些自定义的应用程序特定的JSON数据给客户端?

我正在使用gorilla/sessions来处理会话。在存储值之后,需要调用func (s *CookieStore) Save(r *http.Request, w http.ResponseWriter, session *Session) error。

但是另一方面,这个处理函数还需要通过fmt.Fprintf(http.ResponseWriter, string(js))向客户端发送一些JSON数据。

如何实现这一点?

英文:

Is it possible to send both session and some custom app specific json data to the client form net/http package of Go.

I am using gorilla/sessions for session. And after storing values, needs to call func (s *CookieStore) Save(r *http.Request, w http.ResponseWriter, session *Session) error.

But on the other hand this handler function also needs to send some JSON data to the client by fmt.Fprintf(http.ResponseWriter, string(js)).

How can this be achieved?

答案1

得分: 1

季节通常作为cookie存储,因此它作为头部发送,你需要先保存它,然后再编写你的JSON数据。

此外,你应该考虑使用json.Encoder而不是json.Marshal

func handler(w http.ResponseWriter, r *http.Request) {
    session, _ := store.Get(r, "session")
    // 处理session
    session.Save(r, w)
    w.Header().Set("Content-Type", "application/json")
    enc := json.NewEncoder(w)
    if err := enc.Encode(somedatastruct{}); err != nil {
        // 处理错误
    }
}
英文:

Seasons in general are stored as cookies, so it is sent as a header, you need to save it first then write your json data.

Also you should look into using json.Encoder instead of json.Marshal.

func handler(w http.ResponseWriter, r *http.Request) {
	session, _ := store.Get(r, "session")
	// stuff with session
	session.Save(r, w)
	w.Header().Set("Content-Type", "application/json")
	enc := json.NewEncoder(w)
	if err := enc.Encode(somedatastruct{}); err != nil {
		//handle err
	}
}

答案2

得分: 0

是的,你可以同时写入两者。与会话关联的 cookie 写入响应头部,JSON 数据写入响应体。只需确保先保存会话,因为一旦应用程序开始写入响应体,对响应头部的更改将被忽略。

假设 r 是 *http.Request,w 是 http.ResponseWriter,d 是包含 JSON 的 []byte,那么你应该这样做:

session.Save(r, w)
w.Write(d)

如果你使用标准的 encoding/json 包,那么你可以直接将其编码到响应体中。此外,你应该设置内容类型。

session.Save(r, w)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(yourData); err != nil {
   // 处理错误
}
英文:

Yes, you can write both. The cookie associated with the session is written to the response header. The JSON data is written to the response body. Just be sure to save the session first as changes to the response headers are ignored once the application starts to write the response body.

Assuming that r is the *http.Request, w is the http.ResponseWriter and d is a []byte containing the JSON, then you should:

session.Save(r, w)
w.Write(d)

If you are using the standard encoding/json package, then you can encode directly to the response body. Also, you should set the content type.

session.Save(r, w)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(yourData); err != nil {
   // handle error
}

huangapple
  • 本文由 发表于 2014年10月1日 07:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/26131865.html
匿名

发表评论

匿名网友

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

确定