How can I read a header from an http request in golang?

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

How can I read a header from an http request in golang?

问题

如果我收到一个类型为 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.

答案1

得分: 108

你可以使用r.Header.Get方法:

func yourHandler(w http.ResponseWriter, r *http.Request) {
    ua := r.Header.Get("User-Agent")
    ...
}
英文:

You can use the r.Header.Get:

<!-- language: go -->

func yourHandler(w http.ResponseWriter, r *http.Request) {
	ua := r.Header.Get(&quot;User-Agent&quot;)
    ...
}

答案2

得分: 22

package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", handler)
	log.Fatal(http.ListenAndServe("localhost:8000", nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s %s %s \n", r.Method, r.URL, r.Proto)
	//遍历所有的头字段
	for k, v := range r.Header {
		fmt.Fprintf(w, "头字段 %q, 值 %q\n", k, v)
	}

	fmt.Fprintf(w, "主机 = %q\n", r.Host)
	fmt.Fprintf(w, "远程地址= %q\n", r.RemoteAddr)
	//获取指定标记的值
	fmt.Fprintf(w, "\n\n查找 \\"Accept\\" 的值 %q", r.Header["Accept"])
}

从浏览器连接到 http://localhost:8000/ 将在浏览器中打印输出。

英文:
package main

import (
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;net/http&quot;
)

func main() {
	http.HandleFunc(&quot;/&quot;, handler)
	log.Fatal(http.ListenAndServe(&quot;localhost:8000&quot;, nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, &quot;%s %s %s \n&quot;, r.Method, r.URL, r.Proto)
	//Iterate over all header fields
	for k, v := range r.Header {
		fmt.Fprintf(w, &quot;Header field %q, Value %q\n&quot;, k, v)
	}

	fmt.Fprintf(w, &quot;Host = %q\n&quot;, r.Host)
	fmt.Fprintf(w, &quot;RemoteAddr= %q\n&quot;, r.RemoteAddr)
	//Get value for a specified token
	fmt.Fprintf(w, &quot;\n\nFinding value of \&quot;Accept\&quot; %q&quot;, r.Header[&quot;Accept&quot;])
}

Connecting to http://localhost:8000/ from a browser will print the output in the browser.

答案3

得分: 11

注意:接受的答案缺少一些信息。

Header 表示 HTTP 头部中的键值对。它被定义为一个映射,其中键是字符串类型,值是字符串类型的数组。

type Header map[string][]string

实际上,r.Header.Get 方法获取与给定键关联的第一个值,即从索引 0 获取第一个字符串。

如果你的头部只有一个值,那么这样做是可以的,但如果它有多个值,你可能会错过一些信息。

例如,User-Agent 头部有多个值与同一个键相关联。

user-agent: ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6)", "AppleWebKit/537.36 (KHTML, like Gecko)", "Chrome/80.0.3987.106 Safari/537.36"]

因此,如果你使用 r.Header.Get("User-Agent"),它将只返回 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6),而不是其他值。

如果你想获取所有的值,可以使用以下方法:

req.Header["User-Agent"]
英文:

Note: The accepted answer is missing some information.

A Header represents the key-value pairs in an HTTP header.
It's defined as a map where key is of string type and value is an array of string type.

type Header map[string][]string

Actually, r.Header.Get gets the first value associated with the given key i.e. it just gets the first string from index 0.

This is okay if your header just have one value but if it has multiple values, you may miss out on some information.

Eg. User-Agent header has multiple values against the same key.

user-agent: [&quot;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6)&quot;,  &quot;AppleWebKit/537.36 (KHTML, like Gecko)&quot;, &quot;Chrome/80.0.3987.106 Safari/537.36&quot;,]

So if you use r.Header.get(&quot;User-Agent&quot;) , it'll return Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) only and not the rest of the values.

If you want to get all the values you can use this:

req.Header[&quot;User-Agent&quot;]

huangapple
  • 本文由 发表于 2017年9月3日 15:49:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/46021330.html
匿名

发表评论

匿名网友

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

确定