From 84eea14230ea69a1bbb35b17a7daed6db493a34b Mon Sep 17 00:00:00 2001 From: Neil Martin Date: Wed, 7 May 2025 17:34:42 +0100 Subject: [PATCH 1/2] fix: Set correct Content-Type for PATCH requests Specified in dev documentation: https://developer.jamf.com/developer-guide/docs/api-style-guide#methods - it should be set for all PATCH requests. Discovered that PATCH requests to /api/v2/patch-software-title-configurations require `application/merge-patch+json` or they will fail. Other endpoints work with that or `application/json`. --- jamf/jamfprointegration/headers.go | 15 +++++++++++++-- jamf/jamfprointegration/headers_test.go | 2 +- jamf/jamfprointegration/preprequest.go | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/jamf/jamfprointegration/headers.go b/jamf/jamfprointegration/headers.go index b8bad7d..6e6488d 100644 --- a/jamf/jamfprointegration/headers.go +++ b/jamf/jamfprointegration/headers.go @@ -32,8 +32,19 @@ const ( // - For URL endpoints starting with "/api", it defaults to "application/json" for the JamfPro API. // If the endpoint does not match any of the predefined patterns, "application/json" is used as a fallback. // This method logs the decision process at various stages for debugging purposes. -func (j *Integration) getContentTypeHeader(endpoint string) string { - j.Sugar.Debug("Determining Content-Type for endpoint", zap.String("endpoint", endpoint)) +func (j *Integration) getContentTypeHeader(endpoint string, method string) string { + j.Sugar.Debug("Determining Content-Type for endpoint", + zap.String("endpoint", endpoint), + zap.String("method", method)) + + // Set this header for all PATCH requests + // https://developer.jamf.com/developer-guide/docs/api-style-guide#methods + if method == "PATCH" { + j.Sugar.Debugw("Content-Type for PATCH request set to application/merge-patch+json", + "endpoint", endpoint, + "method", method) + return "application/merge-patch+json" + } // Set this header for PATCH requests on patch software title configurations if strings.Contains(endpoint, "/api/v2/patch-software-title-configurations/") { diff --git a/jamf/jamfprointegration/headers_test.go b/jamf/jamfprointegration/headers_test.go index 685bf41..2598e9e 100644 --- a/jamf/jamfprointegration/headers_test.go +++ b/jamf/jamfprointegration/headers_test.go @@ -62,7 +62,7 @@ func TestIntegration_getContentTypeHeader(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { j := tt.fields.integration - if got := j.getContentTypeHeader(tt.args.endpoint); got != tt.want { + if got := j.getContentTypeHeader(tt.args.endpoint, ""); got != tt.want { t.Errorf("Integration.getContentTypeHeader() = %v, want %v", got, tt.want) } }) diff --git a/jamf/jamfprointegration/preprequest.go b/jamf/jamfprointegration/preprequest.go index a4a66d7..a790552 100644 --- a/jamf/jamfprointegration/preprequest.go +++ b/jamf/jamfprointegration/preprequest.go @@ -25,7 +25,7 @@ func (j *Integration) prepRequest(req *http.Request) error { j.Sugar.Debugw("LOG-CONTENT-TYPE", "METHOD", req.Method, "URL", req.URL.String()) if req.Method != "GET" && req.Method != "DELETE" { - req.Header.Add("Content-Type", j.getContentTypeHeader(req.URL.String())) + req.Header.Add("Content-Type", j.getContentTypeHeader(req.URL.String(), req.Method)) } req.Header.Add("Accept", j.getAcceptHeader()) From 7e46e225c8613485f05da80eedae4c8c149257d6 Mon Sep 17 00:00:00 2001 From: Neil Martin Date: Fri, 6 Jun 2025 08:22:36 +0100 Subject: [PATCH 2/2] fix: Remove redundant Content-Type setting for PATCH requests --- jamf/jamfprointegration/headers.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/jamf/jamfprointegration/headers.go b/jamf/jamfprointegration/headers.go index 6e6488d..1576cb9 100644 --- a/jamf/jamfprointegration/headers.go +++ b/jamf/jamfprointegration/headers.go @@ -46,12 +46,6 @@ func (j *Integration) getContentTypeHeader(endpoint string, method string) strin return "application/merge-patch+json" } - // Set this header for PATCH requests on patch software title configurations - if strings.Contains(endpoint, "/api/v2/patch-software-title-configurations/") { - j.Sugar.Debugw("Content-Type for PATCH endpoint set to application/merge-patch+json", "endpoint", endpoint) - return "application/merge-patch+json" - } - // TODO change this contains to regex. We want to rule out malformed endpoints with multiple occurances. if strings.Contains(endpoint, "/api/v1/packages/") && strings.Contains(endpoint, "/upload") { j.Sugar.Debugw("Content-Type for packages upload endpoint set to application/octet-stream", "endpoint", endpoint)