Get a form submission from Nextcloud Forms
One script reply has been approved by the moderators Verified
Created by marcel klehr12 562 days ago Picked 43 times
Submitted by nextcloud Bun
Verified 29 days ago
1
import createClient, { type Middleware } from "openapi-fetch";
2

3
export async function main(
4
  nextcloud: RT.Nextcloud,
5
  formId: string,
6
  submissionId: number,
7
) {
8

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

20
  try {
21
    const resp = await client.GET("/ocs/v2.php/apps/forms/api/v3/forms/{formId}/submissions", {
22
      params: {
23
        header: {
24
          "OCS-APIRequest": true,
25
        },
26
        query: {
27
          format: "json",
28
        },
29
        path: {
30
          formId: formId,
31
        },
32

33
      },
34
    });
35
    
36
    const submission = resp.data.ocs.data.submissions.find(s => s.id === submissionId)
37
    
38
    return {
39
      submission,
40
    }
41
  } catch(e) {
42
    console.debug('error', e)
43
  }
44

45
}
Other submissions