diff --git a/README.md b/README.md index d7f5201c..7d54deb4 100644 --- a/README.md +++ b/README.md @@ -105,14 +105,14 @@ header.Set("Accept", "application/json") req.Get("https://www.baidu.com", header) ``` -#### Set Header From Struct -Use `HeaderFromStruct` func to parse your struct +You can also set header from struct, use `HeaderFromStruct` func to parse your struct ``` go type HeaderStruct struct { - UserAgent string `json:"User-Agent"` - Authorization string `json:"Authorization"` - } + UserAgent string `json:"User-Agent"` + Authorization string `json:"Authorization"` +} +func main(){ h := HeaderStruct{ "V1.0.0", "roc", @@ -120,9 +120,9 @@ type HeaderStruct struct { authHeader := req.HeaderFromStruct(h) req.Get("https://www.baidu.com", authHeader, req.Header{"User-Agent": "V1.1"}) +} ``` - -Note: Please add tag 'json' to your argument in struct to let you customize the key name of your header +> Note: Please add tag 'json' to your argument in struct to let you customize the key name of your header ## Set Param Use `req.Param` (it is actually a `map[string]interface{}`) diff --git a/doc/README_cn.md b/doc/README_cn.md index d5405bb2..3f724ea9 100644 --- a/doc/README_cn.md +++ b/doc/README_cn.md @@ -97,6 +97,25 @@ header.Set("Accept", "application/json") req.Get("https://www.baidu.com", header) ``` +你可以使用 `struct` 来设置请求头,用 `HeaderFromStruct` 这个函数来解析你的 `struct` +``` go +type HeaderStruct struct { + UserAgent string `json:"User-Agent"` + Authorization string `json:"Authorization"` +} + +func main(){ + h := HeaderStruct{ + "V1.0.0", + "roc", + } + + authHeader := req.HeaderFromStruct(h) + req.Get("https://www.baidu.com", authHeader, req.Header{"User-Agent": "V1.1"}) +} +``` +> 注:请给你的 struct 加上 json tag. + ## 设置请求参数 Use `req.Param` (它实际上是一个 `map[string]interface{}`) ``` go