HTTP Referrer
The Referrer-Policy HTTP header controls how much referrer information (sent via the Referer header) should be included with requests.
Read more at developer.mozilla.org
Iris uses the Shopify's goreferrer package to expose the Context.GetReferrer() method.
The GetReferrer method extracts and returns the information from the "Referer" (or "Referrer") header and url query parameter as specified in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy.
1
GetReferrer() Referrer
Copied!
Which Referrer looks like that:
1
type Referrer struct {
2
Type ReferrerType
3
Label string
4
URL string
5
Subdomain string
6
Domain string
7
Tld string
8
Path string
9
Query string
10
GoogleType ReferrerGoogleSearchType
11
}
Copied!
The ReferrerType is the enum for a Referrer.Type value (indirect, direct, email, search, social). The available types are:
1
ReferrerInvalid
2
ReferrerIndirect
3
ReferrerDirect
4
ReferrerEmail
5
ReferrerSearch
6
ReferrerSocial
Copied!
The GoogleType can be one of those:
1
ReferrerNotGoogleSearch
2
ReferrerGoogleOrganicSearch
3
ReferrerGoogleAdwords
Copied!

Example

1
package main
2
3
import "github.com/kataras/iris/v12"
4
5
func main() {
6
app := iris.New()
7
8
app.Get("/", func(ctx iris.Context) {
9
r := ctx.GetReferrer()
10
switch r.Type {
11
case iris.ReferrerSearch:
12
ctx.Writef("Search %s: %s\n", r.Label, r.Query)
13
ctx.Writef("Google: %s\n", r.GoogleType)
14
case iris.ReferrerSocial:
15
ctx.Writef("Social %s\n", r.Label)
16
case iris.ReferrerIndirect:
17
ctx.Writef("Indirect: %s\n", r.URL)
18
}
19
})
20
21
app.Listen(":8080")
22
}
Copied!
How to curl:
1
curl http://localhost:8080?\
2
referrer=https://twitter.com/Xinterio/status/1023566830974251008
3
4
curl http://localhost:8080?\
5
referrer=https://www.google.com/search?q=Top+6+golang+web+frameworks\
6
&oq=Top+6+golang+web+frameworks
Copied!
Last modified 1yr ago
Export as PDF
Copy link
Contents
Example