如何在golang中从http请求中读取标头?
问题描述:
如果收到类型为 http.Request
的请求,如何读取特定标头的值?在这种情况下,我想将jwt令牌的值从请求标头中拉出.
If I receive a request of type http.Request
, how can I read the value of a specific header? In this case I want to pull the value of a jwt token out of the request header.
答
您可以使用 r.Header.Get :
func yourHandler(w http.ResponseWriter, r *http.Request) {
ua := r.Header.Get("User-Agent")
...
}