diff --git a/pkg/uhttp/wrapper.go b/pkg/uhttp/wrapper.go index 9093baa54..9b1fc4faf 100644 --- a/pkg/uhttp/wrapper.go +++ b/pkg/uhttp/wrapper.go @@ -617,7 +617,24 @@ func (c *BaseHttpClient) NewRequest(ctx context.Context, method string, url *url } for k, v := range h { - headers[k] = v + switch { + // special check for Content-Type and Accept header + case strings.EqualFold(k, "content-type") || strings.EqualFold(k, "accept"): + current := headers[k] + if current == "" { + headers[k] = v + continue + } + // if the incoming value carries extra information + // we respect that and set it as the new value for that + // header + split := strings.Split(v, ";") + if len(split) > 1 { + headers[k] = v + } + default: + headers[k] = v + } } } diff --git a/pkg/uhttp/wrapper_test.go b/pkg/uhttp/wrapper_test.go index e429ace3c..6f9e0ba96 100644 --- a/pkg/uhttp/wrapper_test.go +++ b/pkg/uhttp/wrapper_test.go @@ -414,6 +414,32 @@ func TestWrapper_NewRequest(t *testing.T) { err: nil, }, }, + { + name: "POST request with JSON body with specific content-type header value", + method: http.MethodPost, + url: "http://example.com", + options: []RequestOption{WithHeader("Content-Type", "application/json;odata=verbose"), WithJSONBody(exampleBody), WithAcceptJSONHeader()}, + expected: expected{ + method: http.MethodPost, + url: "http://example.com", + headers: http.Header{"Accept": []string{"application/json"}, "Content-Type": []string{"application/json;odata=verbose"}}, + body: io.NopCloser(exampleBodyBuffer), + err: nil, + }, + }, + { + name: "POST request with JSON body with specific Accept header value", + method: http.MethodPost, + url: "http://example.com", + options: []RequestOption{WithHeader("Accept", "application/json;odata=verbose"), WithJSONBody(exampleBody), WithAcceptJSONHeader()}, + expected: expected{ + method: http.MethodPost, + url: "http://example.com", + headers: http.Header{"Accept": []string{"application/json;odata=verbose"}, "Content-Type": []string{"application/json"}}, + body: io.NopCloser(exampleBodyBuffer), + err: nil, + }, + }, } for _, tc := range test {