Nextcloud WebDAV: Get the content of a file

Get file contents of a file in Nextcloud

Script nextcloud Verified

by marcel klehr12 ยท 7/23/2024

The script

Submitted by nextcloud Bun
Verified 86 days ago
1
import axios from "axios";
2

3
export async function main(
4
  nextcloud: RT.Nextcloud,
5
  path: string,
6
) {
7

8
  const res = await axios.get(
9
    `${nextcloud.baseUrl}/remote.php/dav/files/${nextcloud.userId}/${path}`,
10
    {
11
      auth: {
12
        username: nextcloud.userId,
13
        password: nextcloud.token,
14
      },
15
    },
16
  );
17
  if (res.status !== 200) {
18
    throw new Error(res.statusText)
19
  }
20
  return res.data
21
}
Other submissions
  • Submitted by marcel klehr12 Deno
    Created 568 days ago
    1
    import * as wmill from "npm:windmill-client@1";
    2
    import axios from "npm:axios";
    3
    
    
    4
    // fill the type, or use the +Resource type to get a type-safe reference to a resource
    5
    // type Postgresql = object
    6
    
    
    7
    export async function main(
    8
      nextcloudResource: string,
    9
      userId: string | null = null,
    10
      path: string,
    11
      useAppApiAuth: boolean = false,
    12
    ) {
    13
      const ncResource = await wmill.getResource(
    14
        nextcloudResource,
    15
      );
    16
      const res = await axios.get(
    17
        `${ncResource.baseUrl}/remote.php/dav/files/${userId}/${path}`,
    18
        {
    19
          auth: {
    20
            username: userId || ncResource.username,
    21
            password: ncResource.password,
    22
          },
    23
          ...(useAppApiAuth && ({
    24
            headers: {
    25
              "AA-VERSION": "2.3.0",
    26
              "EX-APP-ID": "flow",
    27
              "EX-APP-VERSION": "1.0.0",
    28
              "AUTHORIZATION-APP-API": btoa(
    29
                `${userId || ncResource.username}:${ncResource.password}`,
    30
              ),
    31
            },
    32
          })),
    33
        },
    34
      );
    35
      if (res.status !== 200) {
    36
        throw new Error(res.statusText)
    37
      }
    38
      return res.data
    39
    }
    40