Nextcloud WebDAV: Upload or update a file

Set 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
  data: string,
7
) {
8

9
  try {
10
    const res = await axios.request(
11
      {
12
        method: "PUT",
13
        url: `/remote.php/dav/files/${nextcloud.userId}/${path}`,
14
        baseURL: nextcloud.baseUrl,
15
        data,
16
        headers: {
17
          Authorization: `Basic ${btoa(`${nextcloud.userId}:${nextcloud.token}`)
18
            }`,
19
        },
20
      },
21
    );
22
    return res.data;
23
  } catch (e) {
24
    console.log(e);
25
    console.log(e.response.data);
26
  }
27
}
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
      data: string,
    12
      useAppApiAuth: boolean = false,
    13
    ) {
    14
      const ncResource = await wmill.getResource(
    15
        nextcloudResource,
    16
      );
    17
      try {
    18
        const res = await axios.request(
    19
          {
    20
            method: "PUT",
    21
            url: `/remote.php/dav/files/${userId}/${path}`,
    22
            baseURL: ncResource.baseUrl,
    23
            data,
    24
            headers: {
    25
              Authorization: `Basic ${
    26
                btoa(`${userId || ncResource.username}:${ncResource.password}`)
    27
              }`,
    28
            },
    29
            ...(useAppApiAuth && ({
    30
              headers: {
    31
                "AA-VERSION": "2.3.0",
    32
                "EX-APP-ID": "flow",
    33
                "EX-APP-VERSION": "1.0.0",
    34
                "AUTHORIZATION-APP-API": btoa(
    35
                  `${userId || ncResource.username}:${ncResource.password}`,
    36
                ),
    37
              },
    38
            })),
    39
          },
    40
        );
    41
        return res.data;
    42
      } catch (e) {
    43
        console.log(e);
    44
        console.log(e.response.data);
    45
      }
    46
    }
    47