Skip to content

Commit bd8b92c

Browse files
JakeFromTheDarkKamil Jakubovic
andauthored
Add only one Content-Type header (raboof#12)
* Add only one Content-Type header Co-authored-by: Kamil Jakubovic <kamil.jakubovic@sap.com>
1 parent 3f88e76 commit bd8b92c

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

configuration_example.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# loadbalance: true
1111
# compression_level: 9
1212
# format: "json_lines"
13+
# content_type: "text/plain"
1314
# max_retries: 3
1415
# timeout: 90 seconds
1516
# tls:

http/client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,13 @@ func (conn *Connection) execRequest(method, url string, body io.Reader, headers
303303
return 0, nil, err
304304
}
305305
if body != nil {
306-
conn.encoder.AddHeader(&req.Header)
306+
conn.encoder.AddHeader(&req.Header, conn.ContentType)
307307
}
308308
return conn.execHTTPRequest(req, headers)
309309
}
310310

311311
func (conn *Connection) execHTTPRequest(req *http.Request, headers map[string]string) (int, []byte, error) {
312312
req.Header.Add("Accept", "application/json")
313-
req.Header.Add("Content-Type", conn.ContentType)
314313
for key, value := range headers {
315314
req.Header.Add(key, value)
316315
}

http/enc.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type bodyEncoder interface {
1818
type bulkBodyEncoder interface {
1919
bulkWriter
2020

21-
AddHeader(*http.Header)
21+
AddHeader(*http.Header, string)
2222
Reset()
2323
}
2424

@@ -56,8 +56,12 @@ func (b *jsonEncoder) Reset() {
5656
b.buf.Reset()
5757
}
5858

59-
func (b *jsonEncoder) AddHeader(header *http.Header) {
60-
header.Add("Content-Type", "application/json; charset=UTF-8")
59+
func (b *jsonEncoder) AddHeader(header *http.Header, contentType string) {
60+
if (contentType == "") {
61+
header.Add("Content-Type", "application/json; charset=UTF-8")
62+
} else {
63+
header.Add("Content-Type", contentType)
64+
}
6165
}
6266

6367
func (b *jsonEncoder) Reader() io.Reader {
@@ -101,8 +105,12 @@ func (b *jsonLinesEncoder) Reset() {
101105
b.buf.Reset()
102106
}
103107

104-
func (b *jsonLinesEncoder) AddHeader(header *http.Header) {
105-
header.Add("Content-Type", "application/x-ndjson; charset=UTF-8")
108+
func (b *jsonLinesEncoder) AddHeader(header *http.Header, contentType string) {
109+
if (contentType == "") {
110+
header.Add("Content-Type", "application/x-ndjson; charset=UTF-8")
111+
} else {
112+
header.Add("Content-Type", contentType)
113+
}
106114
}
107115

108116
func (b *jsonLinesEncoder) Reader() io.Reader {
@@ -169,8 +177,12 @@ func (b *gzipEncoder) Reader() io.Reader {
169177
return b.buf
170178
}
171179

172-
func (b *gzipEncoder) AddHeader(header *http.Header) {
173-
header.Add("Content-Type", "application/json; charset=UTF-8")
180+
func (b *gzipEncoder) AddHeader(header *http.Header, contentType string) {
181+
if (contentType == "") {
182+
header.Add("Content-Type", "application/json; charset=UTF-8")
183+
} else {
184+
header.Add("Content-Type", contentType)
185+
}
174186
header.Add("Content-Encoding", "gzip")
175187
}
176188

@@ -225,8 +237,12 @@ func (b *gzipLinesEncoder) Reader() io.Reader {
225237
return b.buf
226238
}
227239

228-
func (b *gzipLinesEncoder) AddHeader(header *http.Header) {
229-
header.Add("Content-Type", "application/x-ndjson; charset=UTF-8")
240+
func (b *gzipLinesEncoder) AddHeader(header *http.Header, contentType string) {
241+
if (contentType == "") {
242+
header.Add("Content-Type", "application/x-ndjson; charset=UTF-8")
243+
} else {
244+
header.Add("Content-Type", contentType)
245+
}
230246
header.Add("Content-Encoding", "gzip")
231247
}
232248

0 commit comments

Comments
 (0)