Skip to content

Commit 4d04c7b

Browse files
committed
added SetReqContentType
1 parent 3c55805 commit 4d04c7b

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

interceptors/content_type.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
package interceptors
22

3-
const (
4-
ContentTypeHeaderValue = "application/json; charset=utf-8"
3+
import (
4+
"net/http"
55
)
66

7-
// const (
8-
// contentTypeHeaderName = "Content-Type"
9-
// )
7+
const ContentTypeHeaderValue = "application/json; charset=utf-8"
108

11-
// TODO: add ReqContentType interceptor:
12-
// if req.Body != nil && req.Body != http.NoBody {
13-
// req.Header.Set(contentTypeHeaderName, i.ContentType)
14-
// }
9+
const contentTypeHeaderName = "Content-Type"
10+
11+
// SetReqContentType sets Content-Type header to 'v' for requests with not empty body.
12+
func SetReqContentType(client *http.Client, v string) error {
13+
return SetReq(client, setContentType(v))
14+
}
15+
16+
func setContentType(v string) ReqUpdater {
17+
return func(req *http.Request) error {
18+
if req.Body != nil && req.Body != http.NoBody {
19+
req.Header.Set(contentTypeHeaderName, v)
20+
}
21+
22+
return nil
23+
}
24+
}

0 commit comments

Comments
 (0)