Description

While it is possible to get random set headers, the "Host" header containing the requested host (domain / virtual host / etc.) does not exist. My guess is that something else is consuming that key in the header, but I haven't been able to find where yet.

How to reproduce

package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
)

func main() {
    router := gin.Default()
    router.GET("/", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{
            "host": c.Request.Header.Get("Host"),
            "rate": c.Request.Header.Get("Rate"),
        })
    })
    router.Run()
}

Expectations

$ curl -v -H "Rate:123" localhost:8080/ 
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.74.0
> Accept: */*
> Rate:123
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Date: Tue, 12 Jul 2022 03:35:12 GMT
< Content-Length: 24
< 
* Connection #0 to host localhost left intact
{"host":"localhost:8080","rate":"123"}

Actual result

$ curl -v -H "Rate:123" localhost:8080/ 
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.74.0
> Accept: */*
> Rate:123
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Date: Tue, 12 Jul 2022 03:35:12 GMT
< Content-Length: 24
< 
* Connection #0 to host localhost left intact
{"host":"","rate":"123"}

Environment

  • go version: go1.18 linux/amd64
  • gin version (or commit ref): v1.8.1
  • operating system: Linux

Comment From: eleven26

The Host you said is c.Request.Host, not c.Request.Header.Get("Host").

c.Request.Header.Get("Host") is try to get Host HTTP header.

Comment From: cmol

Well, the Host is one of the parameters of the header, but it is clearly getting parsed and moved. Works like a charm now, thank you!