0

Get URI to download attachment

by
Published Oct 17, 2025

Redirects the client to a URL that serves an attachment's binary data.

Script confluence Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Confluence = {
3
	email: string
4
	apiToken: string
5
	domain: string
6
}
7
/**
8
 * Get URI to download attachment
9
 * Redirects the client to a URL that serves an attachment's binary data.
10
 */
11
export async function main(
12
	auth: Confluence,
13
	id: string,
14
	attachmentId: string,
15
	version: string | undefined,
16
	status: string | undefined
17
) {
18
	const url = new URL(
19
		`https://${auth.domain}/wiki/rest/api/content/${id}/child/attachment/${attachmentId}/download`
20
	)
21
	for (const [k, v] of [
22
		['version', version],
23
		['status', status]
24
	]) {
25
		if (v !== undefined && v !== '' && k !== undefined) {
26
			url.searchParams.append(k, v)
27
		}
28
	}
29
	const response = await fetch(url, {
30
		method: 'GET',
31
		headers: {
32
			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
33
		},
34
		body: undefined
35
	})
36
	if (!response.ok) {
37
		const text = await response.text()
38
		throw new Error(`${response.status} ${text}`)
39
	}
40
	return await response.text()
41
}
42