Skip to content

Commit ecf7cda

Browse files
authored
feat(#432): Implement Easier Adding of Multiple Value HTTP Header (#452)
1 parent 3f7a6e5 commit ecf7cda

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

request.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,22 @@ func (r *Request) SetHeaders(headers map[string]string) *Request {
117117
return r
118118
}
119119

120+
// SetHeaderMultiValues sets multiple headers fields and its values is list of strings at one go in the current request.
121+
//
122+
// For Example: To set `Accept` as `text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8`
123+
//
124+
// client.R().
125+
// SetHeaderMultiValues(map[string][]string{
126+
// "Accept": []string{"text/html", "application/xhtml+xml", "application/xml;q=0.9", "image/webp", "*/*;q=0.8"},
127+
// })
128+
// Also you can override header value, which was set at client instance level.
129+
func (r *Request) SetHeaderMultiValues(headers map[string][]string) *Request {
130+
for key, values := range headers {
131+
r.SetHeader(key, strings.Join(values, ", "))
132+
}
133+
return r
134+
}
135+
120136
// SetHeaderVerbatim method is to set a single header field and its value verbatim in the current request.
121137
//
122138
// For Example: To set `all_lowercase` and `UPPERCASE` as `available`.

request_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,19 @@ func TestSetHeaderVerbatim(t *testing.T) {
13711371
assertEqual(t, "value_standard", r.Header.Get("Header-Lowercase"))
13721372
}
13731373

1374+
func TestSetHeaderMultipleValue(t *testing.T) {
1375+
ts := createPostServer(t)
1376+
defer ts.Close()
1377+
1378+
r := dclr().
1379+
SetHeaderMultiValues(map[string][]string{
1380+
"Content": []string{"text/*", "text/html", "*"},
1381+
"Authorization": []string{"Bearer xyz"},
1382+
})
1383+
assertEqual(t, "text/*, text/html, *", r.Header.Get("content"))
1384+
assertEqual(t, "Bearer xyz", r.Header.Get("authorization"))
1385+
}
1386+
13741387
func TestOutputFileWithBaseDirAndRelativePath(t *testing.T) {
13751388
ts := createGetServer(t)
13761389
defer ts.Close()

0 commit comments

Comments
 (0)