Nextcloud Tables: Create a row in a table
One script reply has been approved by the moderators Verified

Create a row in a table in Nextcloud Tables

Created by marcel klehr12 595 days ago Picked 16 times
Submitted by nextcloud Bun
Verified 21 days ago
1
import createClient, { type Middleware } from "openapi-fetch";
2

3
interface Data {
4
  [p: number]: any
5
}
6

7
export async function main(
8
  nextcloud: RT.Nextcloud,
9
  tableId: number,
10
  data: Data,
11
) {
12

13
  const client = createClient<paths>({ baseUrl: nextcloud.baseUrl });
14
  const authMiddleware: Middleware = {
15
    async onRequest({ request, options }) {
16
      // fetch token, if it doesn’t exist
17
      // add Authorization header to every request
18
      request.headers.set("Authorization", `Basic ${btoa((nextcloud.userId) + ':' + nextcloud.password)}`);
19
      return request;
20
    },
21
  };
22
  client.use(authMiddleware);
23

24
  const resp = await client.POST("/index.php/apps/tables/api/1/tables/{tableId}/rows", {
25
    params: {
26
      header: {
27
        "OCS-APIRequest": true,
28
      },
29
      query: {
30
        format: "json",
31
      },
32
      path: {
33
        tableId: tableId,
34
      },
35

36
    },
37
    body: {
38
      data: data
39
    },
40
  });
41

42
  return resp.data;
43

44
}
Other submissions