Repository: gogf/gf

Issue:
ghttp: Can support setting default values ​​for header?
### Is your feature request related to a problem?

Option Yes

### Describe the solution you'd like

I want to set a default value when the header information is empty

### Describe alternatives you've considered


```golang
# now is 
language := r.GetHeader("lang")
if language == "" {
    language = "zh-CN"
}

# replace is
r.GetHeader("lang", "zh-CN") 
```


### Additional

_No response_

Discussion:
@gqcn: Good proposal!@github-actions: Hello @nilsir. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!
你好 @nilsir。我们喜欢您的提案/反馈，并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献，并期待对其进行审查。

Solution patch:
diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go
index e17857f44..6bb4f622d 100644
--- a/net/ghttp/ghttp_request.go
+++ b/net/ghttp/ghttp_request.go
@@ -151,8 +151,13 @@ func (r *Request) IsExited() bool {
 }
 
 // GetHeader retrieves and returns the header value with given `key`.
-func (r *Request) GetHeader(key string) string {
-	return r.Header.Get(key)
+// It returns the optional `def` parameter if the header does not exist.
+func (r *Request) GetHeader(key string, def ...string) string {
+	value := r.Header.Get(key)
+	if value == "" && len(def) > 0 {
+		value = def[0]
+	}
+	return value
 }
 
 // GetHost returns current request host name, which might be a domain or an IP without port.



Test patch:
diff --git a/net/ghttp/ghttp_z_unit_feature_request_test.go b/net/ghttp/ghttp_z_unit_feature_request_test.go
index bc60d8f50..fb200d938 100644
--- a/net/ghttp/ghttp_z_unit_feature_request_test.go
+++ b/net/ghttp/ghttp_z_unit_feature_request_test.go
@@ -363,7 +363,11 @@ func Test_Params_Basic(t *testing.T) {
 func Test_Params_Header(t *testing.T) {
 	s := g.Server(guid.S())
 	s.BindHandler("/header", func(r *ghttp.Request) {
-		r.Response.Write(r.GetHeader("test"))
+		r.Response.Write(map[string]interface{}{
+			"without-def":   r.GetHeader("no-header"),
+			"with-def":      r.GetHeader("no-header", "my-default"),
+			"x-custom-with": r.GetHeader("x-custom", "my-default"),
+		})
 	})
 	s.SetDumpRouterMap(false)
 	s.Start()
@@ -374,8 +378,14 @@ func Test_Params_Header(t *testing.T) {
 		prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
 		client := g.Client()
 		client.SetPrefix(prefix)
-
-		t.Assert(client.Header(g.MapStrStr{"test": "123456"}).GetContent(ctx, "/header"), "123456")
+		client.SetHeader("x-custom", "custom-value")
+
+		resp := client.GetContent(ctx, "/header")
+		t.Assert(gjson.New(resp).Map(), g.Map{
+			"without-def":   "",
+			"with-def":      "my-default",
+			"x-custom-with": "custom-value",
+		})
 	})
 }