-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheader.go
More file actions
62 lines (52 loc) · 1.28 KB
/
header.go
File metadata and controls
62 lines (52 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package grequest
import (
"fmt"
"net/http"
)
type Header struct {
Client *HTTPClient
}
// Sets the header with io.Reader
func (c *HTTPClient) Header() *Header {
return &Header{Client: c}
}
// Gets http client object
func (c *Header) client() *HTTPClient {
return c.Client
}
// Gets raw http.Header from body
func (c *Header) get() *http.Header {
return &c.client().response().Header
}
// Gets header and convert in string map
func (c *Header) GetWithStringMap() map[string]string {
var headersMap = make(map[string]string)
headers := *c.get()
for key, values := range headers {
for _, value := range values {
headersMap[fmt.Sprintf("%s", key)] = fmt.Sprintf("%s", value)
}
}
return headersMap
}
// Gets header string by key
func (c *Header) Get(key string) string {
return c.client().res.Header.Get(key)
}
// Delete header by key
func (c *Header) Del(key string) *HTTPClient {
c.Client.request.header.Del(key)
return c.client()
}
// Sets header key with string value
func (c *Header) Set(key string, value string) *HTTPClient {
c.Client.request.header.Add(key, value)
return c.client()
}
// Sets headers from string map object
func (c *Header) Add(header *map[string]string) *HTTPClient {
for k, val := range *header {
c.Client.request.header.Add(k, val)
}
return c.client()
}