Get a form submission from Nextcloud Forms

Script nextcloud Verified

by marcel klehr12 · 9/2/2024

The script

Submitted by nextcloud Bun
Verified 91 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
  • Submitted by marcel klehr12 Deno
    Created 573 days ago
    1
    import * as wmill from "npm:windmill-client@1"
    2
    import axios from "npm:axios"
    3
    
    
    4
    export async function main(
    5
      nextcloudResource: string,
    6
      userId: string|null = null,
    7
      formHash: string, 
    8
      submissionId: number,
    9
      useAppApiAuth: boolean = false,
    10
    ) {
    11
      const ncResource = await wmill.getResource(
    12
        nextcloudResource,
    13
      );
    14
    
    
    15
      const url = ncResource.baseUrl + '/ocs/v2.php/apps/forms/api/v2/submissions/' + formHash
    16
      console.debug('url', url)
    17
      const config = {
    18
        ...(!useAppApiAuth && ({
    19
          auth: {
    20
            username: ncResource.username,
    21
            password: ncResource.password,
    22
          },
    23
        })),
    24
        headers: {
    25
          //'authorization' : 'basic ' + btoa(ncResource.username + ':' + ncResource.password),
    26
          'content-type': 'application/json',
    27
          'ocs-apirequest': true,
    28
          ...(useAppApiAuth && ({
    29
            "AA-VERSION": "2.3.0",
    30
            "EX-APP-ID": "flow",
    31
            "EX-APP-VERSION": "1.0.0",
    32
            "AUTHORIZATION-APP-API": btoa(
    33
              `${userId || ncResource.username}:${ncResource.password}`,
    34
            ),
    35
          })),
    36
        },
    37
      }
    38
      console.debug('config', config)
    39
      try {
    40
        const resp = await axios.get(url, config)
    41
        console.debug('RESPONSE', resp.data)
    42
        const submission = resp.data.ocs.data.submissions.find(s => s.id === submissionId)
    43
        //console.debug('SUBMISSION', submission)
    44
        return {
    45
          submission,
    46
        }
    47
      } catch(e) {
    48
        console.debug('error', e)
    49
      }
    50
    }