From 144547ea99d6e7461de343fb94e657213e04e5b0 Mon Sep 17 00:00:00 2001 From: Jorge Javier Araya Navarro Date: Mon, 9 Jun 2025 16:37:00 -0600 Subject: [PATCH] Avoid overriding specific Content-Type and Accept headers For some APIs, like Share Point REST API, providing additional information on the Content-Type can determine if the request fails or success (given that the actual content is what the API expects). This change prevents the SDK from blindly overwriting values for those two headers. --- pkg/uhttp/wrapper.go | 20 +++++++++++++++++++- pkg/uhttp/wrapper_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/pkg/uhttp/wrapper.go b/pkg/uhttp/wrapper.go index bb7337b22..a28df72e2 100644 --- a/pkg/uhttp/wrapper.go +++ b/pkg/uhttp/wrapper.go @@ -11,6 +11,7 @@ import ( "net/http" "net/url" "os" + "strings" "syscall" "time" @@ -558,7 +559,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 {