1
Nextcloud WebDAV: Upload or update a file
One script reply has been approved by the moderators Verified

Set contents of a file in Nextcloud

Created by marcel klehr12 179 days ago Viewed 365 times
0
Submitted by nextcloud Bun
Verified 10 days ago
1
import * as wmill from "windmill-client";
2
import axios from "axios";
3

4
type Nextcloud = {
5
  baseUrl: string,
6
  password: string,
7
  username: string
8
};
9

10
export async function main(
11
  ncResource: Nextcloud,
12
  userId: string | null = null,
13
  path: string,
14
  data: string,
15
  useAppApiAuth: boolean = false,
16
) {
17

18
  try {
19
    const res = await axios.request(
20
      {
21
        method: "PUT",
22
        url: `/remote.php/dav/files/${userId || ncResource.username}/${path}`,
23
        baseURL: ncResource.baseUrl,
24
        data,
25
        headers: {
26
          Authorization: `Basic ${
27
            btoa(`${userId || ncResource.username}:${ncResource.password}`)
28
          }`,
29
        },
30
        ...(useAppApiAuth && ({
31
          headers: {
32
            "AA-VERSION": "2.3.0",
33
            "EX-APP-ID": "flow",
34
            "EX-APP-VERSION": "1.0.0",
35
            "AUTHORIZATION-APP-API": btoa(
36
              `${userId || ncResource.username}:${ncResource.password}`,
37
            ),
38
          },
39
        })),
40
      },
41
    );
42
    return res.data;
43
  } catch (e) {
44
    console.log(e);
45
    console.log(e.response.data);
46
  }
47
}
Other submissions