Skip to content

Commit f8acca0

Browse files
authored
fix: Set correct Content-Type for all PATCH requests (#12)
* 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`. * fix: Remove redundant Content-Type setting for PATCH requests
1 parent 09ca725 commit f8acca0

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

jamf/jamfprointegration/headers.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ const (
3232
// - For URL endpoints starting with "/api", it defaults to "application/json" for the JamfPro API.
3333
// If the endpoint does not match any of the predefined patterns, "application/json" is used as a fallback.
3434
// This method logs the decision process at various stages for debugging purposes.
35-
func (j *Integration) getContentTypeHeader(endpoint string) string {
36-
j.Sugar.Debug("Determining Content-Type for endpoint", zap.String("endpoint", endpoint))
35+
func (j *Integration) getContentTypeHeader(endpoint string, method string) string {
36+
j.Sugar.Debug("Determining Content-Type for endpoint",
37+
zap.String("endpoint", endpoint),
38+
zap.String("method", method))
3739

38-
// Set this header for PATCH requests on patch software title configurations
39-
if strings.Contains(endpoint, "/api/v2/patch-software-title-configurations/") {
40-
j.Sugar.Debugw("Content-Type for PATCH endpoint set to application/merge-patch+json", "endpoint", endpoint)
40+
// Set this header for all PATCH requests
41+
// https://developer.jamf.com/developer-guide/docs/api-style-guide#methods
42+
if method == "PATCH" {
43+
j.Sugar.Debugw("Content-Type for PATCH request set to application/merge-patch+json",
44+
"endpoint", endpoint,
45+
"method", method)
4146
return "application/merge-patch+json"
4247
}
4348

jamf/jamfprointegration/headers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestIntegration_getContentTypeHeader(t *testing.T) {
6262
for _, tt := range tests {
6363
t.Run(tt.name, func(t *testing.T) {
6464
j := tt.fields.integration
65-
if got := j.getContentTypeHeader(tt.args.endpoint); got != tt.want {
65+
if got := j.getContentTypeHeader(tt.args.endpoint, ""); got != tt.want {
6666
t.Errorf("Integration.getContentTypeHeader() = %v, want %v", got, tt.want)
6767
}
6868
})

jamf/jamfprointegration/preprequest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (j *Integration) prepRequest(req *http.Request) error {
2525

2626
j.Sugar.Debugw("LOG-CONTENT-TYPE", "METHOD", req.Method, "URL", req.URL.String())
2727
if req.Method != "GET" && req.Method != "DELETE" {
28-
req.Header.Add("Content-Type", j.getContentTypeHeader(req.URL.String()))
28+
req.Header.Add("Content-Type", j.getContentTypeHeader(req.URL.String(), req.Method))
2929
}
3030

3131
req.Header.Add("Accept", j.getAcceptHeader())

0 commit comments

Comments
 (0)