fix: prepend /wiki context path in toAbsoluteUrl for Cloud attachment downloads#99
Closed
adamtan945 wants to merge 1 commit intopchuri:mainfrom
Closed
Conversation
… downloads The REST API returns _links.download as a path relative to the Confluence context root (e.g. /download/attachments/<id>/<file>?version=...). On Atlassian Cloud the context root is /wiki, so toAbsoluteUrl must prepend webUrlPrefix before handing the URL to axios. Previously it went straight through buildUrl, producing https://<domain>/download/... which returns 404 on Cloud. This made `confluence attachments --download` completely unusable against any Cloud instance while listing still worked. The fix reuses the existing webUrlPrefix (already set on line 38) and guards against double-prefixing if the API ever starts returning paths that already include /wiki. Server/DC behavior is unchanged because webUrlPrefix is an empty string when apiPath does not start with /wiki/. Adds three targeted tests covering Cloud download URL construction, double-prefix guard, and Server/DC backward compatibility.
This was referenced Apr 11, 2026
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #98.
confluence attachments <pageId> --downloadalways fails withError: Request failed with status code 404on Atlassian Cloud, even though listing attachments on the same page works correctly.Root cause
The REST API returns each attachment's
_links.downloadas a path relative to the Confluence context root, e.g.:On Atlassian Cloud the context root is
/wiki, so the correct absolute URL is:But
downloadAttachmentcallstoAbsoluteUrl(attachment._links?.download)(lib/confluence-client.js:864), which goes straight throughbuildUrland produces:Missing the
/wikiprefix. Cloud returns 404 for that URL.The interesting part: the constructor already computes
this.webUrlPrefix = this.apiPath.startsWith('/wiki/') ? '/wiki' : ''on line 38, and it's correctly prepended in four places when building web UI links (lines 416, 510, 1356, 1360). But it was never applied insidetoAbsoluteUrl, so attachment downloads (and thedownloadLinkfield on formatted attachments, line 2063) fell through the gap.Query parameters (
version,modificationDate,cacheVersion) are all preserved correctly — the original issue hypothesis about missing query params was wrong. The only thing missing is the context path.Fix
Prepend
webUrlPrefixinsidetoAbsoluteUrlwhen the input is a relative path and doesn't already include the prefix. Server/DC behavior is unchanged becausewebUrlPrefixis an empty string on those instances.The double-prefix guard (
!pathOrUrl.startsWith(\${prefix}/`)) protects against future API changes where_links.downloadmight start returning paths that already include/wiki`.Tests
Added three targeted tests to
tests/confluence-client.test.js:/wikito a relative download path with full query string/wiki/webUrlPrefix) leaves relative paths untouched — regression guardFull suite: 174 passed / 174 total (was 171 before).
Test plan
npx jest tests/confluence-client.test.js— 103/103 passed (3 new)npx jest— 174/174 passednpm link, ranconfluence attachments <pageId> --download --dest /tmp/outagainst a live Atlassian Cloud page, confirmed the attachment downloaded successfully (byte-identical to the file fetched viacurldirectly against the REST API's_links.download).Error: Request failed with status code 404, confirming the reproduction.