From 94b7e1321afe03dab03d925eaddb58c9edf937f3 Mon Sep 17 00:00:00 2001 From: m1no Date: Wed, 24 Sep 2025 21:40:21 +0000 Subject: [PATCH] Fix to meet api specification for empty bodies It should be generally acknowledged that returning an empty body for context-type application/json is not a good practice but some Confluence API endpoints do this and sadly the chance of Atlassian changing this is low so we have to deal with it. --- src/publish/confluence/api/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/publish/confluence/api/index.ts b/src/publish/confluence/api/index.ts index 065033967d3..a3110e49961 100644 --- a/src/publish/confluence/api/index.ts +++ b/src/publish/confluence/api/index.ts @@ -381,10 +381,19 @@ export class ConfluenceClient { }; } - private handleResponse(response: Response) { + private async handleResponse(response: Response) { if (response.ok) { if (response.body) { - return response.json() as unknown as T; + // Some Confluence API endpoints return successfull calls with no body while using content-type "application/json" + // example: https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-content-restrictions/#api-wiki-rest-api-content-id-restriction-byoperation-operationkey-bygroupid-groupid-get + // To prevent JSON parsing errors we have to return null for empty bodies and only parse when there is content + let data = await response.text(); + + if (data === "") { + return null as unknown as T; + } else { + return JSON.parse(data) as unknown as T; + } } else { return response as unknown as T; }